Intonation is based on psychoacoustics, the study of how the human ear and brain perceive sound. The process by which we hear and differentiate between multiple simultaneous sounds influences the musical concept of harmony, which in turn influences the intonation of the notes used to create harmonies. A brief summary of the science behind it all is coming up next, if you're willing to take my word for it. If not, there are many resources on the web explaining this rich and fascinating field in detail.
Sound is caused when something vibrates in the air. (OK, technically sound is caused by vibration in any medium, but if a tree falls into a liquid methane lake on Titan and nobody is there to hear it... anyway, let's stick with air). The frequency (or pitch) of a sound is determined by the number of vibrations per second of the object causing the sound, which is measured in Hertz (abbreviated Hz). Due to biology (and a little bit of psychology), if the frequencies of two sounds are very close to a small whole-number ratio they sound pleasing when heard together. In music theory, this characteristic is generally described as "consonance". For example, two sounds with frequencies of 440 Hz and 220 Hz have a ratio of 2:1, which is a highly consonant interval (one octave). When the relationship between the frequencies of two sounds cannot be described as a small, whole-number ratio, they generally sound tense, or "dissonant", when heard together.
When most objects (including guitar strings) vibrate, they have a "fundamental" frequency that our brain interprets as the basic pitch of the sound. However, there are also a number of smaller vibrations known as "overtones" or "harmonics" which are exact multiples of the fundamental frequency. The number and intensity of these harmonics is a major factor shaping the different tonal qualities of different instruments. If you lightly touch a guitar string at exactly the point where one of these harmonics crosses the string, it will mute the fundamental and any lower harmonics while allowing the selected harmonic to sound. A picture is worth a thousand words, and there's an excellent picture here.
Here's an experiment you can try with an electric guitar and a tuner that can handle harmonics. Some older guitar tuners will only register an open string that's somewhere close to the standard tuning for that string, but most chromatic tuners will work. Tune the guitar to standard tuning and play the open A string. If your tuner shows the frequency of the tone, it will be 110 Hz. Play the harmonic at the 12th fret. This is 1/2 the distance between the nut and bridge so the tone you hear is 2x the frequency of the open string, which is exactly one octave. If your tuner displays frequencies, it will read 220 Hz. Now, play the harmonic near the 5th fret. This is 1/4 of the distance between the nut and bridge so the tone you hear is 4x the frequency of the open string (exactly 2 octaves above). Your tuner will read 440 Hz if it displays the frequency. OK, no big surprises so far.
Now, play the harmonic near the 7th fret. This is 1/3 the distance between the nut and bridge, so the tone you hear is exactly 3x the frequency of the open string. Your tuner will read 330 Hz if it displays the frequency, but in any case it will read as a couple cents sharp of E. Don't believe your tuner? Play the harmonic near the 5th fret of the low E string. This is 1/4 the distance between the nut and bridge so the sound you hear is 4x the frequency of the open E string (exactly two octaves above). If you play the two harmonics together (E string 5th fret and A string 7th fret), you should hear the characteristic "beating" sound that indicates the two notes are slightly out of tune. Why? Well, a quick glance at the table below shows that a "true" E is 329.60 Hz. Huh?
OK, so if the As are at 110, 220, 440 etc., why can't we just make E an exact 3:2 ratio to A (165, 330, 660, and so on)? Well, we could, but then B would sound out of tune. So make B an exact 3:2 ratio to E (123.75, 247.5, 495) and so on. See where this is going? Say you start with A at 220 Hz. By the time you get all the way around the cycle of fifths, you would end up with the next A at 446.0030364990234375 Hz, which isn't even close to the true 2:1 octave (220 and 440). And if the octave is out of tune, fuggedaboudit!
If the math doesn't convince you, try this. Turn off the tuner, play the harmonics at the 5th fret on E and the 7th fret on A and re-tune the A string until the beating disappears. Now play the harmonics of the 5th fret on A and the 7th fret on D, and tune the D string to match the A string. Ditto for D and G. Press down the G string at the 4th fret and tune B to that. Play the harmonics of the 5th fret on B and the 7th fret on the high E and tune the E string to match the B string. Now check the tuning of the high E with the tuner and it will be way flat. (Incidentally, this is also why it's a bad idea to tune your axe using harmonics, even during on-stage emergencies. It's usually better to spend a few extra minutes doing it right than to spend the rest of the night trying to figure out why you're always slightly out of tune).
So how do you solve that problem? Turns out there are many ways, most of which fall into two broad categories of intonation (or temperment). "Just" temperment is used by many Asian instruments and was used by western instruments until the Renaissance, give or take a few years. "Equal" temperment is used by most modern western instruments.
"Just" temperment sets the tuning of scale intervals according to small, whole-number frequency ratios. One possible example:
The advantage of just temperment is that most harmonies relative to the root are extremely consonant. The first time you hear a major triad on the tonic using the just intonation in the table above, you won't want to go back to equal temperemnt. You'll want to sell your piano and buy a fretless guitar. Unfortunately, the disadvantage of just temperment is that some harmonies between notes other than the root are actually quite dissonant, and modulating between keys is virtually impossible. The first time you hear a "minor triad" on the supertonic using the same table, you'll be glad you didn't sell the piano. The ratio of the fourth to the second is 32:27 and the ratio of the sixth to the second is 40:27, which sounds kind of ugly. However, different ratios between the tones can be used to allow consonance according to the melodic and harmonic requirements of the piece. This can be easily achieved using instruments that aren't limited to discrete pitches (for example, trombones, fretless guitars and orchestral strings such as the violin, viola, cello and double bass), or an instrument with variable intonation (such as the sitar, which has moveable frets for exactly this purpose). This would obviously be impractical with a piano, and difficult with a fretted guitar unless you're David Gilmour, or have his level of mastery with string bends.
"Equal" temperment, on the other hand, divides the octave into intervals with exactly the same ratio between each interval. For example, in a 12-tone chromatic scale using equal temperment, the ratio of each semitone to the semitone immediately below it is the 12th root of 2 (approximately 1.0594630943 with a whole bunch more decimal places). The advantage of equal temperment is that every interval is exactly the same regardless of the root note (for example, the ratio of E to A is exactly the same as the ratio of F to B flat). This allows extremely complex harmonies, and infinite, seamless modulation between key signatures. The drawback to equal temperment is that except for the octave, none of the intervals are an exact whole-number ratio, and therefore all of them sound just a little bit dissonant. (Remember the difference between the true E harmonic on the 5th fret of the low E string vs. the slightly sharp E harmonic on the 7th fret of the A string)? If most of your life has been spent listening to western music played on modern western instruments, your ears have been trained so it sounds "close enough" and you probably don't even notice it. If not, you might have wondered why all western music sounds a little bit out of tune no matter what key it's played in. Well, that's why.