The answer is due to two factors: (a) harmonic series and (b) the formation of beats.
If you pluck a string or vibrate the air in a wind instrument, you predominantly hear one note or frequency. But in reality, there are additional notes/frequencies being produced. The note we predominantly hear is called the "fundamental frequency," and the additional frequencies produced are called "harmonic frequencies." The harmonic frequencies are all integer multiplies of the fundamental frequency. (For example, an A at 440 Hz will have as its harmonic frequencies these pitches: 440 x 2 = 880 Hz, 440 x 3 = 1320 Hz, 440 x 4 = 1760 Hz.) So playing a single note on the piano actually produces a series of frequencies, which is called the "harmonic series" in physics. Harmonic series are pleasing to the ear, and these harmonics are not as loud and higher in frequency than the fundamental frequency.
Now that we understand harmonic series, we can consider the second important phenomenon: beats. If you simultaneously play two different pitches with frequencies fA and fB, then a third frequency fbeat = fB – fA can be heard. This "virtual" frequency fbeat indicates how often the "crest" of one sound wave fA overlaps with the "trough" of the other sound wave fB in such a way as to cancel each other out. For example, we could draw the two frequencies fA and fB in blue and red, overlap them, and see that there are times they will cancel. The resultant wave/combination of the two is shown on the bottom:

This phenomenon of the waves periodically destroying each other is referred to as "beats," and it arises from something in physics called the superposition principle. When we say that the beat frequency is 20 Hz, we mean that, 20 times a second, the two waves cancel each other out and we hear nothing. Because the beat frequency fbeat is given by the difference between the two notes fA and fB, the beat frequency fbeat is always lower than the two notes fA and fB.
(In reality, sound waves do not have crests and troughs, but instead have compressions and rarefactions/expansions. Also, beats won't always be audible to humans, but we'll ignore that point and focus only on beats that are above 20 Hz and thus audible to human ears.)
Here's what we have so far:
- harmonic frequencies are integer multiples of a fundamental frequency and are pleasing to the ear;
- when you play two notes together, a third frequency is produced
Consonant intervals are two notes just far enough apart in frequency that the virtual third tone (the beat frequency) completes a harmonic series with the two real tones. For example, if you play two notes fA = 100 Hz and fB = 150 Hz, then the beat frequency will be fbeat = 150 – 100 = 50 Hz. This beat frequency completes a harmonic series because tone A, 100 Hz, is 2 x 50 Hz and tone B, 150 Hz, is 3 x 50 Hz. Both notes (100 Hz and 150 Hz) are integer multiples of the beat frequency, and so they are consonant intervals. And what exactly are these notes, 100 Hz and 150 Hz? They are roughly Ab and Eb (a perfect fifth apart) in the lower register:

So the trick for producing consonant intervals is to have the beat frequency fbeat produce a virtual fundamental frequency that completes a harmonic series with the two real notes. Stated differently, a consonant interval is produced whenever the two notes being played have a difference in frequency fbeat = fB – fA, where fB ÷ fbeat and fA ÷ fbeat are both integers (or close to integers).
As it turns out, if you pick a particular register of the piano, there is a particular interval that tends to satisfy this condition to produce a virtual fundamental frequency. For example, in the lower register, perfect fifths tend to satisfy the condition, exactly as our example above shows. (100 Hz and 150 Hz occur in the lower register and are roughly a perfect fifth apart.) In higher registers, smaller intervals tend to satisfy the condition.