Equal temperament is a compromise everyone agreed to stop arguing about. The argument ran for about two thousand years, it was never resolved so much as abandoned, and the number twelve is a very good rational approximation that happens to be small enough to fit on a keyboard, and nothing more than that.
Pluck a string. It vibrates at some frequency, call it f. It also vibrates, more quietly, at 2f, 3f, 4f, and so on: the harmonic series. Your ear is built to hear these together as one note with a timbre, rather than as a chord, which is a nice trick and also the source of every problem in this post.
The ratio 2:1 is the octave. Two notes an octave apart sound like the "same" note, because every harmonic of the higher one is already a harmonic of the lower. The ratio 3:2 is the perfect fifth, and it's the next most consonant interval because the harmonics overlap almost as well. 4:3 is the fourth, 5:4 the major third.
So the plan, since at least Pythagoras, was: build a scale out of these nice small ratios, because small ratios sound good, and that isn't a matter of taste, it's a matter of overlapping harmonics.
Here is the problem. Start on a note and go up by fifths: multiply by 3/2, twelve times. You get (3/2)^12 ≈ 129.746. Now go up by octaves seven times: 2^7 = 128. Musically, twelve fifths should land you back on the note you started on, seven octaves up. They do not. The ratio between where you land and where you should be is 531441/524288, about 1.0136, and it's called the Pythagorean comma.
It is small. About a quarter of a semitone. It's also unavoidable, and the reason is not musical at all. A power of 3/2 can never equal a power of 2, because 3^n is odd and 2^m is even. No integer solution exists. The circle of fifths is really a spiral, and it misses by a comma every lap.
Every tuning system in history is a decision about where to hide that comma.
Pythagorean tuning hid it all in one place. Tune eleven fifths pure, and the twelfth, the "wolf" fifth, is out by the full comma. It howls. You just don't write music that goes there.
Meantone temperament, the Renaissance answer, spread the error over the fifths to make the major thirds pure instead, because thirds had become more important than fifths in the music people wanted to play. The result: some keys are gorgeous, and some are unusable, and every keyboard had a fixed set of good keys and bad ones. Composers knew which was which. Key choice meant something.
Well temperament, the eighteenth-century answer, spread the error unevenly so that every key was usable but each had a slightly different character. This is the "well" in the Well-Tempered Clavier. Bach wasn't demonstrating equal temperament, which is a common misreading. He was demonstrating that a good uneven spread lets you play in all twenty-four keys without any of them being broken, while each still sounds like itself.
Equal temperament spreads the error perfectly evenly. Every semitone is exactly 2^(1/12), about 1.05946. Every fifth is 700 cents instead of the pure 701.955. Every major third is 400 cents instead of the pure 386.3, which is a lot, nearly 14 cents sharp, and if you play a pure third next to an equal-tempered one the difference is obvious. Equal temperament makes every key equally, mildly wrong. No wolves. No character. Total freedom of modulation, paid for by no interval being quite in tune.
That is the compromise. It won because pianos are expensive, orchestras are large, and nobody wanted to retune between pieces. It's a standard the way a floating-point format is a standard: every value is slightly off, but they're all off in a way everyone has agreed to, so the errors do not compound across systems.
Now the actual question. Why divide the octave into twelve, rather than ten, or nineteen, or a hundred?
The requirement is: find an integer n such that some number of equal steps, k out of n, approximates the fifth well. In other words, k/n should be close to log2(3/2) ≈ 0.58496. This is a Diophantine approximation problem, and the way to solve it's continued fractions.
The convergents of log2(3/2) are:
1/1, 1/2, 3/5, 7/12, 24/41, 31/53, ...
Each is the best rational approximation with a denominator that size or smaller. The denominators are the candidate scale sizes: 2, 5, 12, 41, 53. Twelve is where the approximation gets good (7 steps out of 12 approximates the fifth to within 2 cents) while the denominator stays small enough that you can name the notes and build the instrument.
The next real improvement is 53. Fifty-three equal divisions of the octave gives a fifth within a tenth of a cent of pure and thirds that are very nearly just. It is, by the numbers, a much better system. A man named Bosanquet built a 53-tone harmonium in 1876. It had ninety-odd keys per octave in a staggered layout and you can see it in a museum in London. It is exactly as popular as you would expect a ninety-key octave to be.
Nineteen and thirty-one also show up historically as attempts at better thirds. They are playable. Nobody plays them.
So twelve is not sacred. It is the smallest denominator that gets the fifth close enough, and "close enough" was set by ears, and the ears were satisfied, and the arguing stopped.
The odd coda is that once everyone tuned to twelve equal steps, the ear adapted. Listeners raised on equal temperament often hear a pure major third as slightly flat, because they have learned the sharp one as correct. The compromise became the reference. The standard rewired the thing it was approximating.
Which is, I think, the actual moral. Standards do not win by being right. They win by being adopted, and then adoption makes them right, because right is whatever the ears expect.
There's a whole world of music, Turkish makam, Indian raga, Indonesian gamelan, that never signed the treaty and still uses intervals that would make a piano tuner sweat. It sounds "out of tune" to a Western ear for the same reason a pure third does. It isn't. It just signed a different treaty.