Firstly, write down the order of sharps. This goes F,C,G,D,A,E,B. - Many different mnemonics exist to remember these:
Funky Cows Get Drunk And Eat Breakfast Food
Father Charles Goes Down And Ends Battle
Fat Cats Go Down Alleys Eating Bugs
Fat Cats Get Dinner After Eating Bread
Fidel Castro Gets Drunk At Every Bar
Fred Can't Go Driving After Eight Beers
Four Chorus Girls Danced All Evening Bare
Fair Cinderella Goes Down And Eats Bugs
- Make your own up and you'll remember it! (Notice also how they go up in 5ths).
Now, separately, write down the order of flats. The goes B,E,A,D,G,C,F. - Notice it's the order of sharps backwards (so you only have to remember it once!)
Battle Ends And Down Goes Charles' Father
Bread Eating After Dinner Gets Cats Fat
British European Airlines Do Great Continental Flights
Now. On each diagram, write a 0 (zero) above C, and number each letter ascending from left to right, looping round to end up with 7 back on C. - This corresponds to MAJOR keys. Similarly, underneath each letter, write a separate row of number, starting this time with 0 (zero) on A. This corresponds to MINOR keys (recall C major and A minor both have no sharps or flats in their key signature).
You should end up 2 diagrams like this:
Sharp diagram:

and Flat diagram:

Now it works like this. For major keys, use the top row of each diagram, for minor keys, use the bottom row. The number equals the number of sharps (for the FCGDAEB diagram) or flats (for the BEADGCF diagram) in the key signature.
Notice, the numbers to the right of each zero are in bold on the diagrams. - These correspond to 'normal' keys (A,B,C,D,E,F,G). For keys starting on a sharp (eg. C# major, A# minor) use the numbers to the left of each zero on the sharp (FCGDAEB) diagram. For keys starting on a flat (eg. Bb major, Eb minor), use the numbers to the left of each zero on the flat (BEADGCF) diagram.
Once you know the number of flats or sharps in a particular key, you can find out which ones they are by counting along this number from left to right on the relevant diagram.
So, some examples:
A major. - This key does not start on a flat or sharp, so look for a number in bold (to the right of each zero) in the top line of the diagrams (we use the top line because it's major). - We see the number 3 appears in bold on the sharp diagram. This means it has 3 sharps in it. (Sharps because we're on the sharp diagram). Now, which 3 sharps? - We count along on the sharp diagram; F,C,G. - these are the first 3 sharps, therefore A major has F, C and G sharps in its key signature. (Notice it couldn't be 4 on the flat diagram since that number is to the left of the zero (on C), not to the right).
F major. - Again, this does not start on a flat or sharp, so look for a number in bold in the top line of each diagram. We see number 1 appears in bold on the flat diagram. Therefore F major has one flat. - Which flat? - Well it must be Bb since this is the first flat in the diagram.
D minor. - Again, doesn't start on a flat or sharp, so look for a bold number in the bottom line of each diagram. (We use the bottom line because it's minor). We can see the number 1 appears below D on the flat diagram, therefore D minor must have 1 flat in it, and that must be Bb.
G# minor. - This key starts on a sharp, so we will be using the numbers to the left of the zero on the bottom line of the sharp diagram. (The numbers not in bold). - So, we see the number 5 below the G. G# minor must have 5 sharps in it, and these 5 are... F,C,G,D,A (the first 5 sharps from left to right).
Minor keys:
Remember, in the harmonic minor, the 7th note of the scale is raised one semitone. This is NOT included in the key signature, but is written as an accidental on the relevant notes. Thus, in our D minor example, although we now know that the key signature only has one flat in it, we must also remember to raise the 7th, so in this case, C becomes C#.
(Similarly, in the melodic minor, the 6th and 7th of the scale are both raised one semitone when ascending, but when you're descending, you keep the note as it appears in the key signature).
Note also the concept of 'relative' major and 'relative' minor. These are keys which share the same key signature. - So, F major and D minor is one example (both have one flat in the key signature.) We say F is the 'relative major' of D minor, and D minor is the 'relative minor' of F. - Notice the relative major is always a minor 3rd (3 semitones) above the minor, and the relative minor is always a minor 3rd (3 semitones) below the major.
This may seem complex at first, but once you've done it a few times, it really is one of the easiest ways I know of identifying a key signature that you don't know.