I'll try to explain why we choose those 7 notes out of 12. Note, that this is just my opinion and not a scientific and proven research.

According to observations by ancient Greeks, the sounds whose frequency have simple ratios of small natural numbers like 2/3 or 4/3 or 5/4 sound harmonically and pleasing together. Except for number 7 (I don't know why). So, if we take several such sounds we expect to get a nice sounding chord.

For example, in a C major chord (C + E + G) the frequencies ratio of E to C is 5/4, ratio of G to C is 3/2 and ratio of G to E is 6/5. So, if we denote frequency of C as 4f, then the frequencies of C, E, G are 4f, 5f and 6f. Those are indeed ratios of small numbers and notes in C major chord are considered in harmony with each other.

By the way, if we invert the chord as G, C, E we get ratios of 3f, 4f and 5f. These are the smallest numbers that we can use to build a 3-note chord. Does it mean that inverted C chord is the most perfect and sounds most harmonical? As a person without an ear for music I don't understand.

Because of this ancient people made scales that consisted of notes with good ratios. For example, if we take first 6 notes of a major scale (C, D, E, F, G, A), the frequencies would be f, 9/8f, 5/4f, 4/3f, 3/2f, 5/3f. Except for D, all of these are small numbers and if ancient Greeks were right, the song made from them should sound harmonical. D also has good ratios with some notes like G (D/G ratio is 3/4).

But this scale has a disadvantage, that one cannot easily raise or lower the pitch. Different people have different voice ranges, but if you have an instrument with these notes, you cannot change the key and transpose the melody. For example, if you had a sequence of C, D, F then you cannot pick higher or lower notes with same ratios.

That's why people invented an equally tempered scale. It turned out that if you divide an octave into 12 equally spaced notes, then 7 of them are pretty close to a traditional major scale. That's why it is 2212221 and not something else. With equally tempered scale one can transpose a song up or down easily, keeping the same ratios between notes.

By the way, note B seems to use different pitches in different scales. There is a space between frequencies of A and C, so there is a place for a note, but there is no simple ratio one could use for B and different scales used different pitches for the note.

You can read more about pitch ratios here [1].

While major chord sounds "most" harmonical, but there are only 3 major chords in a major scale, and the music tends to be boring if it uses only perfect ratios. I guess that's why musicians started to use more and more "imperfect" chords over time. For example, in a minor chord like A, C, E ratios are 10f:12f:15f. And indeed a minor chord sounds like a wrong version of a major chord. If we take a chord with 7th, the ratios get even worse, and some musicians use chords with notes that do not belong to a normal chord, or even do not belong to the key.

By the way, non-harmonical notes are ones that differ only by a half tone - for example, E and F (ratio is 16:15) or three tones (ratio is 36:25).

[1] http://www.swarthmore.edu/NatSci/ceverba1/Class/e5_2006/Musi...