Music Theory/Finding the Key and Mode of a Piece
Finding the proper key is a matter of finding the I note, then finding what mode the notes seem to follow. This can be easy or difficult, depending on the piece of music. Some music does not really follow a mode at all, but non-modal key signatures are frowned upon.
The first place to start would be the chord structure. The most important chord is the I chord. This is the chord formed by the first, third, and fifth notes in a mode. This is why a C major chord is called C major, because the C major scale starts with C D E F G. Every mode naturally contains three major chords, three minor chords, and one diminished triad. By "naturally", it is meant that all of the notes in the chord are in the mode's scale. D major is not natural in the key of C because a D major consists of D, F#, and A, and F# does not occur in the C major scale. One major chord can be extended to a dominant seventh chord (in the key of C major, this would be G7), and all of the minor chords can be extended to a minor seventh chord.
Suppose we have a song and have determined it to have these chords: C, F, and G. This is a classic example of a three-chord song, of which the twelve-bar blues is a good example (but there are many other possible three-chord structures). Suppose the C chord occurs often and it is also the first and last chord in the song — it is probably the I chord, because the tendency is to begin on the I chord, move around a bit, and come back ("resolve") to the I chord. C, F, and G happen to be the three major chords that occur naturally in the C major scale. Therefore the chord progression is in C major and it is quite likely that any melody accompanying it is also in C major.
But what if a chord progression contains six major chords? Not all of those can occur naturally in a given mode, since each mode has only three. Does the mode change during the progression? Well, yes and no. Certainly, the key signature won't change with each chord change; it will only change when the piece as a whole is changed to another key, generally when a new chord becomes the I chord.
By the way, in case you forgot, harmonic minor and melodic minor scales use the same key signatures as the natural minor scale.
There is another way to figure out the key signature, which does not depend on matching the scale exactly. You still have to find the 1 note and write the notes of the scale. Let's say your song uses only five notes: 1 2 4 5 b7. (This is a Japanese scale.) It has a flatted seventh; everything else is the same as in the major scale. What modes have a flattened seventh? Everything except lydian and ionian. The one with the fewest sharps/flats of these is mixolydian. Therefore, this scale fits well into the mixolydian mode, and a piece written in this scale would have the appropriate key signature for the mixolydian mode.
Now let's try a different Japanese scale: 1 2 b3 5 b6. This one is even easier: what modes have a flatted third and a flatted sixth? Aeolian and locrian. But locrian does not have a 5 note, it has a b5 note instead. Therefore this piece uses the key signature for the natural minor scale.
Now a really puzzling one: 1 b3 4 b5 5 b7. What mode has a flatted third, fifth, and seventh? Locrian. But again this scale has the 5 note, which locrian does not! This scale doesn't fit into anything. There are two possibilities: the song is indeed in locrian and the 5 is outside the scale. Notes outside of a scale in a song are rare and usually stand out (sometimes to good effect and sometimes to bad effect, depending somewhat on your musical tastes); if a note is used often it is part of the scale. If you find the 5 note is not part of the scale, then the song is in locrian. However, it turns out this "modified locrian" with the 5 note is very common and it is called the blues scale. (If confronted with this apparent scale, this is the much more likely possibility. Another way to look at this is as the minor pentatonic with a b5. The minor pentatonic is 1 b3 4 5 b7.) The normal way to handle this particular oddball scale is to just interpret it as a major scale. That is, the "F blues" is F major.
More often than not, the bass instrument (typically a cello or electric bass) will play the tonic as the first note of the first measure (excluding pick-ups).
Some music is "atonal", meaning there is no tonal center and therefore no I note. Atonal music has a fairly distinct flavor, and generally does not occur in popular music — even rap music is tonal when it has any musical notes at all. The most well-known atonalists were probably Arnold Schönberg (who, incidentally, abhorred the word "atonal") and, after Schönberg's death, Igor Stravinsky (before Schönberg's death, Stravinsky made many tonal pieces). Atonal music is perhaps the musical equivalent of abstract art, that is, free from the confines of the usual way of things — at the possible cost of an inability to be understood by others because the work does not inspire the same thoughts in them.
What key signature do you use for atonal music? None at all. The very idea of a key implies the presence of a tonal center, which atonal music, by definition, lacks. But be sure that the piece really is atonal before arriving at this conclusion. The twelve-tone technique, which Schoenberg usually used (and Stravinsky used it as well), is almost a dead giveaway that a piece is atonal (but there are some tonal songs that use it).