It's a simple question with quite an involved answer.
First a quick primer on wavelength, frequency and pitch.
Frequency is how many times a vibration repeats in a period of time. Wavelength is the length of a single vibration, which for something like a guitar string, corresponds to the length of time it takes to repeat. You can see that if the wavelength halves, the frequency doubles.
Conveniently, the wavelength of the sound from a vibrating string is proportional to the length of the string, so we can talk about the two almost interchangeably.
Doubling the frequency (halving the wavelength) takes us up an octave.
When you hit/pluck/shake anything, it will vibrate at all kinds of frequency. Most of those frequencies will die away very quickly. Here's why.
These are conceptual pictures of a string vibrating. Think of one end as the nut of a guitar, and the other as the bridge. The bottom one is "not a resonant frequency", because the line doesn't end at the bridge. I could perhaps have drawn the last part of the wave with a steeper curve, so it reached the dot, and that would have shown what happens to waves of that frequency -- rather than work with the length of the string to reinforce itself, it works against the length of the string, gets cancelled out, and dies away.
Now look at the other waves. They sustain because they "fit" into the length of the string. They would carry on vibrating that way forever, if it weren't for the sound board, the friction of the air, and so forth, perhaps the magnetic field of a pickup, taking energy away.
I've shown the 1st, 2nd, 3rd and 4th -- but they keep going on, at various levels of loudness.
The complex waveform of a guitar note is the result of adding all those resonant frequencies together.
Here are some waveforms:
Here is a frequency analysis of single guitar note - eachhorizontal axis is frequency, vertical axis is amplitude. Each spike is a different harmonic.
It's the precise mixture of frequencies, and how quickly each one dies away, that provides the timbre of the instrument.
A good way to get a feel for this is to play with an analogue synth (or a simulation of one). These use "additive synthesis", in which an oscillator produces a pure sin wave, and you build up a timbre by adding in "harmonics" using more oscillators, choosing the relative pitch and volume for each one.
You may have noticed that your guitar sounds "boomier" if you pluck it near the 12th fret, and more trebly when you pluck it near the bridge. That's because when you pluck near the middle of the string, you're giving lots of energy to the 1st harmonic, and very little to the other harmonics.
Now, what happens if you gently touch the midpoint of the string?
- You kill the 1st harmonic. That involved the string moving at exactly the point you've suppressed.
- You allow the 2nd harmonic to keep ringing - the string doesn't need to move at the position you've muted.
- You kill the 3rd harmonic
- The 4th harmonic keeps ringing
- ... and so on.
The result is that you subtract the fundamental note, and a lot of "odd" harmonics, leaving a harmonic that's an octave up, and some of the higher harmonics. The result is a sound with fewer harmonics that therefore sounds "purer".
A great way to get a feel for this is to play the open string, then listen carefully as you gently touch the midpoint, to see how you achieve the octave by subtracting part of the sound.