This is potentially a long answer. But to start -
An octave is two pitches vibrating at ratio 1:2. So, if C were tuned
to 100Hz (keeping the math simple) the C above is 200Hz and the C below
is 50Hz.
A fifth is two pitches vibrating at a ratio of 2:3. So the C is 100,
G above is 150, the next C is 200, the next G is 300. Notice from G to
G, 150 to 300, the ratio is 1:2. Notice also from G to C, 150 to 200,
is a fourth, which is ratio 3:4.
So, C is 100, C is 200, G is 300, C is 400, E is 500, G is 600. This
is the start of the overtone series, and gives you the pitches of a
major triad. This is why major triad has such a consonant sound, this
is a definition of consonance: pitches with simpler mathmatical ratios,
the more oblique the ratio, the more dissonant the sound.
On to the 8 note scale, from the three fundamental chords of tonic
(C-E-G) dominant (G-B-D) and sub-dominant (F-A-C) you derive the scale.
These are the fundamental chords because, as described above, their
ratios, within themselves and to eachother, are the simplest.
Of course, the piano has 12 notes, this has to do with the history of
adding leading tones to help declare a new tonic (modulation). Webern
has a nice concise essay on this.
And of course, its possible to devide the octave into any number you
chose, or to have scales that avoid the octave. This you find in
micro-tonal music and music outside the Western canon. And there are
many variations of the tuning of the 12 piano notes, equal tempered,
mean tone, etc.
There is a lot to explore in your question.