Levels are (agreed upon) constraints put on an encoder. They limit the encoder to certain frame sizes and bit rates in order to make sure that a decoder can correctly decode such a bitstream. This means that a decoder that claims to be compatible with level X bitstreams must be able to decode a stream encoded with level X. You can find an overview of all H.264 levels on Wikipedia.
If you do not know what level you need, you should think about your target application. Is it going to be a high definition broadcast or a small video for web? Look at the maximum supported frame dimensions (e.g. 1920×1080 vs 320×240) and frame rates (e.g. 60 Hz vs. 15 Hz) and set the appropriate level.
The level itself does not influence the quality or file size. It only enforces a certain upper boundary or gives you a general hint, since logically, a 1080p60 video will be larger than a 320×240 web clip. But generally, you control the quality by setting an average bitrate, or a constant quality level. The level is just secondary here.
The keyframe distance doesn't have anything to do with the above. It is the distance (in pictures) between two I-pictures. There are three types of pictures in video compression:
- I-pictures, which can be decoded without reference to others ("intra-coded").
- P-pictures, which can only be decoded with the information from one or multiple previous P- or I-pictures ("previous" as in display order, P standing for "predicted").
- B-pictures, which can only be decoded with the information from one or multiple previous P- or I-pictures ("previous" as in decoding order, not necessarily display order; B standing for "bidirective").
Here, the P-frame requires the previous I-frame to be decoded first. The B-frame requires the previous P-frame and the following I-frame to be decoded.
The specific implementation of the picture types depends on the codec. The Wikipedia article on Group of Pictures (GOP) also explains that concept from a different perspective: Usually, I-pictures are interleaved with P- and B-pictures, and occur in a fixed interval—the keyframe interval. This is also the GOP length.
A video with only I-pictures will provide the best quality at the highest file size. The longer the GOP gets, the smaller the file will be, since P-pictures or B-pictures require less bits to encode. Longer GOPs are rarely used for streaming, as a lost frame might deteriorate the quality, but in broadcasting, a longer GOP is not unusual.
For a very detailed description of what the picture types mean in H.264, you can also read Overview of the H.264/AVC Video Coding Standard by Thomas Wiegand et al (see chapter IV A).