When music is talked of as being in a particular key it means that the melody is based upon notes of the major scale (or minor scale) with the same name, e.g., in the key of C, C major scale notes (i.e., C, D, E, F, G, A and B) will occur much more frequently than notes that do not belong to the C scale (i.e., sharpened and flattened notes).
In the key of G, G scale notes will be most common (i.e., G, A, B, C, D, E and F# will occur frequently). You will notice here that F# occurs rather than F natural. However, rather than add a sharp to every F note, an easier method is used whereby a sharp sign is placed on the F line (the top one) of the staff at the beginning of each line. This is referred to as the key signature of G major; which contains an F# note.
Here are the key signatures for all the major scales that contain sharps.
The sharp key signatures are summarized in the following table.
Notice as the number of sharps increase, each new key is a fifth higher than the last.
E.g., C to G is a fifth
G to D is a fifth
D to A is a fifth etc.
Also note that as the number of sharps increases each new key contains all the sharps of the previous keys and the additional sharp is always a semitone lower than the root note of the new key, e.g., the key of B contains all the sharps of the previous keys (F#, C#, G#, D#),plus a new sharp A# (a semitone lower than B).
Here are the key signatures for all the major scales that contain flats.
The flat key signatures are summarized in the following table.
As the number of flats increase, each new key is a 5th lower than the last.
E.g., C to F is a fifth
F to Bb is a fifth
Bb to Eb is a fifth etc.
As the number of flats increases, each new key contains all the flats of the previous keys and the additional flat is always a fifth lower than the last additional flat, e.g., the key of Db contains all the flats in the key of Ab (Bb Eb Ab Db) plus an additional flat which is a 5th lower than Db - Gb.
All these major scales can be summarized in the following diagram known as the cycle of fifths.