This is quite a complex matter. I will explain it in a couple of steps.
When two keys are pressed at the same time (this is called an interval), a piano will produce a combined sound. If you listen closely you will hear that this combined sound beats. The volume increases and decreases a few times per second (some intervals beat slower).
Each interval has to beat at the right speed
Each interval on a piano's keyboard beats at a different rate. The rates of the beats for each interval are important. A piano will only sound good if these beats are exactly right. There are quite some rules that these beats have to respect. A major third beats faster than a fifth. An octave only beats very slowly. Also, if you play a major third somewhere on the keyboard, it will beat faster when you play it on a higher pitch (more to the right).
The beats are produced by higher harmonics
Okay, the interval's beats are important but where do they come from? Well, if two tones with almost the same pitch (but different) sound together they will produce a beat. Let's say tone A has pitch FA and tone B has pitch FB. When FB is only a little higher than FA together they will produce a beat with frequency F = FB - FA.