TL;DR: Backwards compatibility, and the predominance of seven-note scales.
We're going to take a musical walk through history...
Let's say you're inventing music, and you start out with a single string playing a single note. That gets boring after a while, but you realize that you can play more notes by shortening the string length, so with a bit of playing around, you come up with a scale that sounds good to you. That scale has only 7 notes in it, because the eighth one ("octave") sounds like the first, only higher. Now you decide to name these 7 notes based on the letters of the alphabet: A, B, C, D, E, F, G. After that, the pitches start to repeat at the octave, so you give them the same names.
Since the idea of zero being a number doesn't really exist yet, you decide to measure the distance between notes by counting them (in other words, you're using ordinal numbers rather than cardinal numbers). So the note you start on is the first note, the note above that is the second, and the note above that is the third, then the fourth, and so forth. For example, if you count to the third note above your starting note, and then, using that as a new first note, you count to the third note above that, you end up with the note that is the fifth note from your starting note. It's a real shame that zero hasn't been invented yet, because now you have 3 + 3 = 5.
Now fast forward a bit and you've decided that, using the same seven notes, C actually sounds like a better place to start and end the scale. Same notes, just a different convention for where you start. You decide to maintain the original naming convention, though, because that's what everyone is used to, and backwards-compatibility is important.
There's one problem with your scale, though. When you picked out the notes that sounded good, you actually picked an unequally-shaped scale. Some of the notes are closer together, and some of the notes are farther apart. This leads to the scale shape you may have seen before: WWHWWWH. As long as you're just playing a melody, you don't really need to worry about this shape (except that it determines how much shorter to make the string) -- everything just sounds good. The trouble comes when you start to get fancy, and play more than one note at a time.
Let's say that you have a simple melody that goes C-D-E-F. You know that notes "a fourth" apart sound good together, so you decide to harmonize this in fourths. When the melody plays a C, you play an F, and it sounds great. That's followed by a D and an G, great! Then comes E and A, still sounding good. But then the melody goes up one more note to F, so your harmony goes up one more note to B, and YUCK! B is the fourth note above F, so it should sound good... What happened?
The problem is that, because your scale is unequally shaped (because that's what sounded good), the distance between E and F is a smaller distance than the distance between A and B, so your nice perfect fourth got all messed up. In order to fix this, you have to kind of fudge your B a little bit, and make it lower. But don't worry, there's plenty of room between A and B to stick your new modified-B. But what are we to call this new note? We can't just go renaming all the notes of our scale every time we need to throw in a new note. So instead, we name it after the note it replaced. Instead of a "B", we have a "lowered B", aka, B-flat.
As we play around with different scales and harmonies, we find all kinds of places where we have to raise and lower notes in order to maintain the proper scale shape. We always name them based on the original note that they're replacing, plus whether it was raised or lowered. In fact, any of our original 7 notes can be raised or lowered. The staff notation still reflects that we are still basically using the same 7-note scale, but depending on where we start, some of those notes may have to be raised or lowered in order to maintain the same scale shape. It's a clever little hack that allows us to maintain backwards compatibility with previous music notation, while "patching" it to allow new pitches that didn't exist before.
Eventually, to simplify things, you decide that when two notes are closer together (like E and F), the raised version of the lower note (E#) is "close enough" to the higher note (F) to pretend like they're the same (and vice versa with the lowered version of the higher note). OTOH, for notes that are further apart, you decide that the raised version of the lower note is "close enough" to the lowered version of the higher note note. This rounding off prevents you from inventing wacky keyboards with gazillions of keys, or fretboards with a large number of impossibly close frets.
Since there are five of these wider gaps, this adds five new notes to our inventory of notes -- causing 12 notes in total. But we still generally tend to use only 7 note scales. As a result, the 5 new notes don't have any inherent name of their own -- they always replace one of the previous 7. By using these modified notes in place of the original, it allows us to play the same scale shape starting on any possible note (including one of the modified notes). Of course, now you have to specify up front which version of any modified notes you are using.