Misconceptions
In the Middle Ages, a monk called Guido of Arezzo created a piece of vocal music known as the Hymn to John The Baptist. The initial words of each line of music start with the syllables: Ut - Re - Mi - Fa - Sol - La - Si. And the lines of music started on those pitches. Because of this piece, he is credited with inventing the note names used in Western music.
By the sixteenth century, 'Ut' had been replaced in some countries by 'Do'.
In the nineteenth century, Sarah Anna Glover changed Si to Ti to enable sol-fa notation with a single letter. And so we get the notes as presented in The Sound of Music: Do - Re - Mi - Fa - So - La - Ti - Do.
Except... Whilst The lyrics were a useful acrostic to help people remember the note names, Guido didn't invent the system of notes. The musical system had been around a long time before Guido Arezzo. He did invent our music notation, but for the note names he just Latinised the existing Arabic note names, which themselves had been derived from the Egyptian note names.
Real Origins
Credit
The Egyptians did not take credit for their musical system. The claimed that they had be taught music by the Neter Thoth, deity of the Moon, wisdom, knowledge, writing, hieroglyphs, science, magic, art and judgment.
Beginnings
In the beginning was Re - as in the Egyptian deity. Re in his aspect as Atem (the self-created one) is represented by the number 1.
Creation
The numbers 2 and 3, Auset and Auser, represent male and female. Creation requires the numbers 2 and 3.

The entire musical system grows out of Re using the ratio 2:3 in length or 3:2 in frequency. The musical interval described by this generating ratio is known as the 'Perfect Fifth'.
First Generation
Creation always requires creating a pair of opposites. Proceeding from Re by a perfect fifth in either direction, we generate the next two notes: La, and Sol; the Dominant and Subdominant.
The first three first notes are the principal notes of both melody and harmony.
Second Generation
Proceeding from Re by two perfect fifths in either direction, we generate the next two notes: Mi and Do.
The first five notes form the pentatonic scale, which is the foundation of melody.
Third Generation
Proceeding from Re by three perfect fifths in either direction, we generate the next two notes: Si and Fa.
This pattern is reflected in the architecture of the Temple of Hathor at Dendera.
These seven notes form the diatonic scale when rearranged in pitch order.
These notes form the Dorian mode, which consists of the keynote and three perfect fifths in either direction. It has 6 forms of symmetry - see if you can spot them. Being neither sharp nor flat, it has a stable and controlled affect.
It is considered to be a representation of perfection/deity, and was used for around 80% of plainchant.
English Note Names
There are two presentations of each diatonic mode: One has the keynote at the bottom with the remaining notes above. Melodic lines can therefore only lead down to the keynote.
The other presentation has the keynote at the centre such that melodic lines may lead down to or up to the keynote. This presentation has the prefix 'hypo' attached to it.
The Hypo-Dorian has this extra symmetry (Three notes above, and three below the keynote), giving seven in total. The Hypo-Dorian was the most commonly used presentation, so much so that it was called 'The Common Mode'.
The notes of the Common Mode if named alphabetically are:
And so we arrive at the English names of the notes as used today.
Sharps and Flats
The notes as derived are not all the same interval apart. Some intervals are (approximately) twice the size of others.

If we continue upwards another seven steps by perfect fifths, we generate the sharps. Notice that they arrive in the same order as the Naturals, with each a semitone higher than its Natural counterpart.
Five of the sharps fit neatly into the gaps in the Naturals, giving twelve equally spaced pitches. (And two coincide with existing pitches.)

Likewise, if we continue downwards by perfect fifths, we generate the flats.
Five of the flats fit neatly into the gaps in the Naturals, giving twelve equally spaced pitches. (And two coincide with existing pitches.)

This also explains the otherwise confusing keyboard layout.
The Full System
The sharps and flats do not perfectly overlay. They are a small distance apart. This gives us 21 commonly used notes per octave. (Some early keyboard instruments have split keys to allow both to be played.)
Again, if we continue up as before we generate the double-sharps, and downwards, the double-flats. This completes the 35 notes per octave available in standard notation.
Pythagoras
Because Pythagoras taught Egyptian music theory to the Greeks, this system is referred to as the 'Pythagorean' system of Music. It is conceptually beautiful being generated entirely by a single ratio.
As an idealist system derived from first principals without reference to musical instruments or acoustics, it may properly be described as platonic.
The pitches of the notes derived by this method do not lend themselves to harmony. When musicians complained about the tuning, Pythagoras was so convinced by this system that he said that it must be the musicians's ears that were wrong. (They weren't.) To use these notes for harmony, the intervals must be 'tempered' such that they are consonant with the keynote. Tuning the notes, or creating a temperament is an entirely separate conversation, best left for another article.
Afterthought
Somebody, somewhere is going to ask "but what about tetrachords?"
If you examine the first three notes, you will see that they form a pair of perfect fourths a tone apart within the octave. These are known as tetrachords. People who promote tetrachords as the origin of modes don't usually describe how these first three notes came about.
In the Dorian scale we can see that there are two intervals (diatonic). And that each fourth is comprised of two tones and one semitone. These intervals could be in the order TTS, TST, or STT. So we can generate modes using any combination of these.
This seems to be a very excellent way of analysing the physical and emotional qualities of modes, but cannot the source of their derivation. Why? The modes used in the medieval church consisted of those starting on D, E, F, and G (plus their hypo presentations). Those starting on D, E, and G have two tetrachords as described above, whilst the mode starting on F does not.
The 25% error-rate just destroys that argument, although it is still possible to think of lots of uses for tetrachords, including the moveable mode system developed by Guido Arrezzo to help people sight-sing, they cannot be the source of the modes. I'll grant you that there are books that include this red-herring, and some people have been taught that way, but the evidence is in the medieval modes themselves. A theory that accounts for only 75% of the data isn't watertight. Moreover, it leaves the question of why you would want a mode made of a pair of fourths a tone apart in the first place. Not nearly good enough, especially when the pythagorean method explains the generation of everything from a single note with one ratio. Occams Razor.
Just to rub salt in the wound, if tetrachords had been the source of the modes, two of the church modes can't be derived from tetrachords, so how did they come about? Moreover tetrachords naturally produce nine modes. Why aren't all nine used? and why don't four of them even have names in western musical theory? For fun, why not calculate all nine and start using them as a new musical resource for composition?