To get the obvious out of the way: "keys" are those regions that are defined by "common practice" tonality. But what does that mean? Not so terribly easy to explain. Here, have a look at the Wikipedia article. They have difficulty defining the meaning of "key" as well.
"Key" is a fundamental concept in traditional harmony. In order to write a piece of music, the first thing you have to do is decide what key it will be in. You also have to decide what time signature, tempo and instruments you are going to use. Maybe pick a title. But all these are secondary to deciding on the key. Here is a bare bones piece in C major:
Here is the "same" piece in a different key, E flat major:
I just used the music software's transposition utility to move the music up a minor third, putting it into E flat. The "key" of a piece of music in traditional harmony is the name of the tonic, the "home base", where the music begins and ends. It is defined harmonically with a perfect authentic cadence, as in both the examples. A C major or E flat major triad on its own does not define anything. To create the sense of a key, you need a cadence. A lot of so-called "tonal" music written nowadays does not have cadences so is only loosely tonal. In the Baroque and Classical periods, every piece and every significant section of every piece HAD to end with a perfect authentic cadence. The only exceptions are the "church keys" found in 17th and some 18th century music where the key signature appears to be missing an accidental and where the composition appears to end not on the tonic, but the dominant. There are lots of examples in Corelli and there is even a Bach prelude in D minor that ends on an A major chord. The reason for this is that this is a holdover from the older modal system. As soon as tonal harmony fully established itself in the first half of the 18th century, these anomalies disappeared.
There are twelve major and twelve minor keys. Ah yes, there are two kinds of keys: major, as shown above, and minor, which share the same key signature but start in a different place. Here is the key of A minor, which has the same key signature as C major, no sharps or flats:
What is that sharp doing there? The problem with minor keys is that they don't have a leading tone, so we have to insert one. The leading tone is the note one semitone below the tonic which leads to the tonic: B leads to C and G# leads to A. G natural is a whole tone below and does not lead to A. The major and minor keys are a very useful relic from the modal system as they derive, respectively, from the Ionian and Aeolian modes.
So that is a rough and ready introduction to the idea of "key". And I've also slipped in the idea of "mode". There is a Wikipedia article on modes as well. Modes have a very long and obscure history that goes right back to the ancient Greeks. They had different kinds of musical structures that they named after different tribes (Dorians) and regions (Lydia). In order to talk about modes and, indeed, to complete the notion of key, I'm going to have to talk about scales.
If you go back and look at my little example in C major, you will discover that it uses all the notes from C to D: C D E F G A B C. This sequence of notes, placed in order, is called a "scale". To write in C major, you use the C scale and to write in E flat major, you use the E flat scale. These are major scales and they all have the same interval structure: tone, tone, semitone, tone, tone, tone, semitone. There are also minor scales that are different because they have a different interval structure. The minor scale is all those notes from A to A: A B C D E F G A. The structure is tone, semitone, tone, tone, semitone, tone, tone. But we can't use exactly this structure because of the lack of a leading tone, so we always have to raise the G to G# in a cadence. The rules about when to raise the leading tone (and sometimes the note before, the F, as well) are complex.
What complicates things even further is that the Greek modes fell completely out of use. In the middle ages, when the monks were developing chant, they also developed a set of modes and they used the Greek names. But the so-called "church modes" have nothing musically to do with the Greek modes, they just use the same names. See the Wikipedia article for more confusing details! The church modes, which include the Ionian and Aeolian, source of our major and minor scales, also include several other kinds of interval structures. Here are the eight church modes with their modern names:
|Click to enlarge|
That little "f" indicates the finalis or note to end on. The modes on the second line, with the prefix "hypo" all end on a middle, not bottom, note. To take just one example, the Dorian mode shares qualities of both the major and minor scales. There is a minor third above the "tonic" so it sounds minor, but the sixth note of the scale is a major, not minor sixth. So it sounds a bit major as well. The Phrygian mode sounds most unusual because it has a leading tone from above--F to E--instead of from below. A lot of the harmonic color of Spanish music comes from using the Phrygian mode.
One way of looking at the relationship between key, mode and scale is to see key and mode as the theoretical structures behind the music, while the scales are what you hear used in the music.
Before leaving this complicated and confusing subject, I should mention that there are scales that lie outside the whole key and modal systems. For example, one scale that was used quite a bit by Debussy is the whole-tone scale, so-called because it consists only of whole tones. No cadences are possible because there are no leading tones. Also, all the chords are augmented chords. Here is what that scale looks like:
But there are other scales as well. Russian music often uses the octatonic scale which has eight notes. They are typically organized alternating tones and semitones like this:
But this does not exhaust the possibilities as there are many other possible scales (or modes). Russian theorists have postulated quite a few interesting modes to describe the music of Shostakovich and based them on traditional Russian concepts of mode. As an example here is what the theorist Alexander Naumovich Dolzhansky calls an "Aeolian double-lowered melodic" mode:
And there are a whole bunch of others!
I think that I will stop here before things get any more complicated. Let's end with a piece of music that Dolzhansky used to illustrate his theories of mode in Shostakovich: the Sonata No. 2 in B minor, op. 61. Here are the first two movements played by Valentina Lisitsa: