The guitar has harmonic notes at some places. I can play it, but I don't understand the logical reason why/how this is can played. Can you tell me how exactly this works?
What other musical instruments have harmonic notes besides the guitar?
Essentially all instruments produce overtones, which are frequencies other than the dominant frequency of the note. When one or more overtones is a multiple of the base (or fundamental) frequency, it's called a harmonic. Some instruments like drums do not generally have harmonic overtones. Others like guitars, violins, and flutes do have have them; here's more info on Wikipedia.
On a guitar, when you strike an open string the wavelength λ of the sound produced is double the length of the string. When you play the twelfth fret, the wavelength is half of that (λ / 2), so the frequency is doubled. The sound produced by the open string actually has that doubled frequency as a harmonic. You can think of it as the string vibrating at both frequencies, as if you were somehow playing both the open string and the twelfth fret at the same time.
In fact, a vibrating guitar string has components at many multiples of the base frequency (call it F). To your ear it still sounds like the fundamental, but mathematically it's more like this:
The higher-frequency elements give the note it's timbre; this is how you can tell two instruments apart, or even tell different kinds of guitar strings apart. For example, the sound where
Imagine a pure sine wave, x, with period T. The note one octave higher is the sine wave y with period T/2. If you add both together that's what you see with the harmonic effect. The frequency of the resulting signal is 1/T with a clear sub-frequency of 2/T.
Note that the second half of the combined wave (x + y) is the inverse of the first half, just like any regular sine wave. The period is still T, but the distinct crests make it "feel" like it has period T/2. Note that normally y would have a smaller amplitude than x, since it is not the fundamental.
As for playing harmonics: When you hit the harmonic at the twelfth fret (for example), the string is still open so it's like you're playing the open string. However, you're also creating a dead spot on the string, similar to fretting. This deadens all of the frequency components that are incompatible with this dead spot, because their vibrations are interrupted. The major frequency (F) does not have a dead spot at the twelfth fret, since its dead spots are at the ends of the string; therefore it does not sound. 2F has a dead spot there, obviously, so it is not deadened. 3F is deadened, since its dead spots are at 1/3 and 2/3 of the string length. 4F has a dead spot in the right place. And so on.
Thus the sound of the harmonic is essentially the following:
You'll note that if we set F2 = 2F, then we get this:
This looks exactly the same as simply fretting the twelfth rather than playing the harmonic, right? Not quite. As Gauthier notes in the comments, the entire guitar string vibrates when you play a harmonic. When you fret a note only the portion between the fret and the base of the string vibrates; when playing a harmonic, the portion between the fret and the head of the string vibrates as well. This affects the coefficients (a, b, c, etc.) of the overtones, and gives harmonics their unique sound.
On guitar and other string instruments, you can create a harmonic at any point on the string. Harmonics are strongest where the string is divided into the smallest equal parts. In other words, the harmonic at the 5th fret is weaker than the harmonic at the 7th fret, which is in turn weaker than the harmonic at the 12th fret. This is because the 5th fret is 1/4 of the way along the string, dividing the string into 4 parts; the 7th is 1/3 of the way, dividing the string into 3; and the 12th is halfway, halving the string. The harmonic at the first fret, for example, may not even be audible to your ear, because so many of the strongest frequencies are eliminated from the "original" sound of the open string.
A concise way to put it is:
The vibrating part of the string vibrates to accomodate for all nodes you apply to it, while keeping a constant distance between all nodes. Additionally, the string vibrates with the least number of nodes possible, since this is what takes the least energy.
When you play an open string, the constraints on the string are only two: the ends of the string where they attach to the instrument (see 1).
When you play a fret normally, your finger and the fret press on the string hard enough to kill the progagation of the wave from one side of your finger to the other. This reduces the "vibrating part of the string" mentionned above. See 2a and 2b, it is as if the string was shorter.
When you play an harmonic tone, your finger touches the string gently enough to let the string vibrates on both sides of your finger, while still inflicting a node. See 3a and 3b.
In the harmonics case there are three constraints (three nodes): the ends of the string and your finger.
If these three constraints are distributed so that dividing the string in three parts accommodates for them, you will hear the tone associated with one third of a string. The timbre will be quite different compared to the fretted third-of-a-string tone, as you'll have the equivalent of three strings vibrating. But the pitch is the same.
Note in 2a and 3a that the pitches (wavelength) of the fretted and harmonics tones are the same. Only the timbre differs.
Note in 3b that you could have put your finger on the other (unfingered) node of the string, and reached the same result.
The higher the number of nodes needed to accommodate for your constraints, the harder it is for the string to vibrates. The resulting wave may be very short, and that prevents the vibration for several reasons:
This explains why harmonics seem to be working only at certain spots.
Other instruments: you certainly know that you can whistle by blowing into a bottle. If you blow harder (easier on smaller bottles), you can get higher tones. While the principle is not exactly the same as for a string, what happens is that the air vibration accommodates for its "nodes", while accommodating for another constraint: the air speed. Above a certain threshold it is just easier (takes less energy) to add nodes to the vibration.
Whether the tone series of brass instruments are harmonics or not is a problem that people disagree about, so I won't go into that one :)
(This is a simplified description, it assumes for example that the string is plucked exactly between two nodes. I believe the principle remains correct.)
The two existing answers are great. Here's one other way to look at things.
Most things resonate; that is, when they vibrate, they prefer certain frequencies to others. Even if you hit, say, a wooden table, some frequencies will die away almost instantly; others will keep going for a short while, before it settles down into silence.
Pitched musical instruments are designed so that unwanted frequencies die away almost instantly, and the frequencies you want to hear keep going. When you pluck a string it vibrates at a load of frequencies, then in a matter of milliseconds it settles to the set of frequencies you want to hear. The energy from the unwanted frequencies is either absorbed by the body of the guitar or goes into fuelling the resonant frequencies.
The frequencies you want (and get) when you pluck a string are:
Each of those is called a harmonic. The root frequency * 2 is the first harmonic. Root * 3 is the second harmonic, and so on.
Those harmonics are playing every time you play a note, whether you consciously try to "play a harmonic" or not.
Now, if you gently place a finger anywhere on the string, you will mute the root frequency; and it's likely you'll mute a lot of the other harmonics too.
If you gently place a finger exactly halfway along the string, you will mute the root frequency, and all the odd-numbered harmonics — but the second, 4th, 6th, 8th, etc. harmonics will all continue to ring.