Helmholtz hypothesized[1] that the dissonance of a pair of sine wave tones was related to these beats. Slow beats sound like a pleasant vibrato effect. Extremely fast beats are not perceived as beats at all, with only two separate tones heard. Only moderately fast beats sound dissonant.
This was confirmed experimentally[2] by Plomp and Levelt.
Sethares generalized this relationship to arbitrary sounds[3], finding that an amplitude-weighted sum of the consonance of all pairs of partials ("partial" meaning one of the sine waves that forms part of the waveform, as can be found by Fourier transform) well approximated perceived consonance.
Most Western musicals instruments are harmonic or approximately harmonic[4]. They produce a waveform with partials of frequencies that are an integer multiple of the lowest frequency partial (called the "fundamental").
Increasing pitch by an octave doubles the frequency of all partials. An integer multiplied by two is still an integer, so if you play harmonic notes separated by octaves the partials will overlap. All pairs of partials will be either identical or far apart, so none form dissonant beats. This maximizes consonance.
But music with only octave intervals would be very boring, so the octave in standard Western music theory is divided into 12 equal parts. This is an excellent choice for harmonic instruments, because it closely approximates several small-integer ratios. The interval of a "fifth" (actually seven steps away in the octave, but music theory uses strange numbering to simplify playing the most common musical styles) is a frequency ratio of 3:2. This results in half the partials overlapping, and the other half still being positioned so they avoid dissonant beats, so the fifth is also highly consonant.
Wikipedia has a graph comparing equal divisions of the octave with small-integer ratios:
https://en.wikipedia.org/wiki/Equal_temperament#/media/File:...
You can see that 12 divisions has many useful approximations. It represents a good balance between complexity and musical utility, so I don't think it's surprising that it became the standard.
But note that small-integer ratios are only consonant with harmonic timbres! If the partials are not integer multiples of the fundamental, as is often the case in tuned percussion, you need a different tuning system. Indonesian classical music[5], which makes heavy use of tuned percussion, is famous for this. You can use Sethares' model to generate tuning systems suitable for arbitrary timbres, e.g. https://sethares.engr.wisc.edu/mp3s/morphine_crystal.html
[0] https://en.wikipedia.org/wiki/Beat_(acoustics)
[1] https://en.wikipedia.org/wiki/Sensations_of_Tone
[2] http://www.lifesci.sussex.ac.uk/home/Chris_Darwin/PerMuSo/pd...
[3] https://sethares.engr.wisc.edu/paperspdf/consonance.pdf