A container is a file format that holds one or more encoded media streams, together with the information needed to play them together. Understanding containers clears up a confusion that trips up nearly everyone: the file extension is not the codec.
The difference between a codec and a container
A codec encodes and decodes a single kind of media, such as H.264 for video or AAC for audio. A container packages the results: an MP4 file, for example, might hold an H.264 video stream and an AAC audio stream side by side. The container is the box; the codecs are what the contents are written in.
What containers hold
Besides the encoded streams, containers carry metadata, titles, descriptions, and timing. Timing matters most: containers store the timestamps that keep audio and video synchronized during playback, so a line of dialogue lands on the right frame.
Interleaving
Streams inside a container are usually interleaved: audio and video are stored in alternating small chunks rather than as two long blocks. Interleaving lets a player read a little of each stream at a time, which keeps playback smooth even when the file is being read over a network or from slow storage.
Established containers
MP4, formally MPEG-4 Part 14, is the most widely used video container and carries many codec combinations. WebM is an open, royalty-free container built for the web, typically holding VP8 or VP9 video with Opus or Vorbis audio. Matroska, often seen with the MKV extension, is a flexible container supporting many codecs and subtitle tracks. WAV is an audio container that usually holds uncompressed PCM, which is why WAV files are large but simple. Ogg is another open container often used with Vorbis and Opus audio.
Why the extension is not the whole story
The extension names the container, not the codec. Two MP4 files can hold entirely different video codecs, and one codec can appear inside several containers. When media fails to play, the useful question is usually not what the file ends with, but which codecs are inside and whether the player supports them.