Gee.. page full of very low quality/BS answers there! Hadn't seen Music StackExchange before.. super non-impressive. The 2 top-voted answers are absolute without-a-clue nonsense, people-who-don't-know rambling aimlessly, not explaining anything.

I am also no music historian, don't know all the history, but:

Not until the third-top-voted answer, the one beginning "Historically, keyboards didn't always work that way" is it mentioned that..keyboards didn't always work that way. But they go on "Our musical notation is older than enharmonic equivalency that you get with "well-tempered" keyboards" - evidently confusing well temperament with equal temperament.

Which is understandable - it seems everyone's 'educated' with that misconception; I certainly was. (Classical then jazz pianist) Even in my 30s I remember reading a book from the 1880s talking in detail about the specific emotional qualities of the different keys, thinking they were just imagining things, deluded. Then I learnt that no, Bach's "well-tempered" actually wasn't our equal temperament, with each note 2^(1/12) higher than the next.[0] Every key (C major, E minor etc) actually sounded different, the intervals were different etc. So that 1880s book wasn't crazy at all. Apparently that's about when equal temperament was brought in everywhere (mid to late 19th C), and the old "different keys sound different" was abolished.[1] Many composers complained about the loss of..key personality, didn't want the new system. Now all the keys sounded exactly the same. (Well, not to me, I've got perfect pitch. But to most people they do now.) It seems a strangely huge cultural forgetting, that somehow I never heard about all that, during decades in various music worlds.

So before the equal temperament system of e.g. middle C is 3 semitones above A, so 220 x 2^(3/12) Hz, many different systems of ratios were used to compute the note frequencies, producing lots of different tunings, 'temperaments'.

The problem in all this is the Pythagorean comma, the gap resulting from the awkward fact that when trying to construct a scale from octaves (2x the frequency) and 5ths (3/2x), 2^x=3^y has no positive integer solution. With continued fractions they worked out that 2^7~(3/2)^12 (128~129.7463..) is a good approximation, which is, in short, why pianos have 12 notes per octave, and going up 12 5ths takes you up 7 octaves. With equal temperament, the gap is spread evenly among the notes, only now none of the intervals have simple ratios of frequencies like they did before (except the octave). There's now nothing perfect about a "perfect 4th" or "perfect 5th".

Anyway..going up in 5ths you get C,G,D,A,E,B,F#..

Going down you get C,F,Bb,Eb,Ab,Db,Gb..

Gb and F# on modern pianos are (just different names for) the same note, but in the pre-equal temperament days, going up a 5th wasnt just Freq x 2^(7/12) but multiplication by some ratio, most simply 3/2. And then the note you got going up (F#) was different to that you got going down (Gb). Some keyboards had black notes split in half, one playing the sharp, one the flat.

But I won't go into more historical detail, because I don't know it and I'm rambling aimlessly enough already. And you could fill a book with the answer to the Q.

[0] well, pedantic piano tuners won't agree, but that's the theory.

[1] Some instruments had been equal temperamentish for centuries, e.g. guitar I think, (it using the same frets for different keys) but others, like pianos, not until then.