Tonality and Timbre
The consonance of musical intervals is maximized when the partials of a given timbre (which for harmonic timbres are also called harmonics or overtones) align with the notes of a given tuning. Many cultures' tuning systems can be seen as attempts to maximize the consonance of music played with the timbres of their dominant instruments. For example, the non-tonal tuning systems of Indonesia, Thailand, and Mandinka Africa maximize the consonance of their dominant instruments' timbres (gongs, ranats, and balafons. respectively).
Likewise, the West's system of tonality can be seen as arising from an attempt to maximize consonance with its dominant instruments' timbres, which are almost universally harmonic. The system of tuning that most closely aligns fundamentals with harmonic partials is Just Intonation.
However, the benefits of consonance are not the only consideration in tonality. The utility of being able to modulate freely among keys is also highly valued, and attaining this benefit—especially on fixed-pitch instruments such as the piano -- requires tempering a tuning away from Just Intonation. Such tempering moves notes out of alignment with their corresponding harmonics, thereby reducing consonance. This reduction in consonance has been vigorously opposed by many musicians and music theorists throughout history, and this opposition continues today.
Read more about this topic: Dynamic Tonality