For some reason, this is a question that many Musicians and Music Educators answer incorrectly. A quick Google search yields a wrong answer. It’s really quite logical and uses a bit of Music Theory knowledge. As long as you know the order of Flats and the order of Sharps, it’s very easy to understand.
Consider that there is ONE key with NO Sharps nor Flats: The Key of C
There are 7 Flats: Bb, Eb, Ab, Db, Gb, Cb, Fb creating the Major Keys of F, Bb, Eb, Ab, Db, Gb and Cb.
That brings the Total so far to 8.
There are 7 Sharps: F#, C#, G#, D#, A#, E#, and B# which give us the Major Keys of: G, D, A, E, B, F#, and C#.
Our Total is now 15.
Each Major Key (the Ionian Mode) has a relative minor (the Aeolian Mode),
……so we need to double the Total, giving us 30 Keys.
Be careful not to confuse the other modes, Dorian, Phrygian, Lydian, Mixolydian and Locrian, with Keys. They are scales that are all based on the Major Scale. Think of them as Tonalities instead of Keys.
Understand that Letter names of pitches must be listed in order in a scale and that all 7 must be used only once. The iii chord in the Key of A Major is C# minor… not Db minor, even though those two chords sound alike.
A C7 chord is spelled C-E-G-Bb NOT C-E-G-A#. Even though an A# and Bb have the same PITCH, they are NOT the same note in terms of a scale or chord. This shows an example of “Enharmonics“; one pitch with two names.
Literacy demands that we spell words correctly for their intended meaning. Although they sound alike, the words “their” and “there” have different meanings.
Music is a Language, therefore the same Rules apply.