Quite often, when I see a pedalboard of guitarists I admire, there is a compressor pedal in the chain. How exactly does this pedal affect the sound?
My explanation is more for generic compressors, but I believe it is relevant for guitars as well.
A compressor is most often used to amplify quiet sounds more than loud sounds. You could think of it as someone turning up the volume on quiet parts, and down on loud parts. This is done by a compressor much faster than a human with a volume knob could :)
As a result, the output has a thinner volume range than the input of the compressor.
Here is an example of a compressor setting:
If there is no compression at all, the output is equal to the input. If you have -20 dB in, you'll have the same -20 dB out. It is represented by the black line in the graph.
With compression as in the example above (green curve):
In other words, the amplification gain varies with the loudness of the input.
Further, this gain is often restricted from changing too fast. This is what attack and release times are for. The attack time prevents the gain from going up to fast, while the release time prevents it from going down too fast.
I feel a bit silly writing a new answer when Jduv's is so good, and so well received, but I'm going to anyway partly because I want to use simpler terms, and partly because I have a point to make about attack.
Imagine you had a signal that was sometimes too loud, and sometimes too quiet. You'd deal with it by turning the volume down when it's too loud, and bringing the volume back up when it's quiet. A compressor does exactly this, except it adjusts the volume automatically, usually very quickly.
At its simplest then, it takes a signal that has a variable volume level, and fixes it up so that the volume is more constant.
Whether this is desirable musically depends on the player and the style of music; it is an artistic choice.
Compressors have a number of adjustable parameters. The following ones address the basics:
Compressors are incredibly versatile, having loads of different uses.
Maybe a little counter-intuitively, a compressor can be used to increase the attack; to make something sound more dynamic and staccato:
Imagine the waveform of a clean guitar note:
If we apply compression, and we set, say, 10ms of attack time, it does this:
So it accentuates the attack of plucking the string. Alternatively, with a really short attack parameter, you could dampen the pluck.
Affordable compression units have side effects on the timbre of the sound. Really high end studio equipment minimises this -- but it doesn't matter to the guitarist, because the side effects are rarely unmusical; indeed they are often pleasant. Some units are desirable precisely because of their side-effects.
A compressor "compresses" the signal that your guitar produces by normalizing the dynamic range of the audio input signal based on a threshold value. This effect is used virtually everywhere in recording. Everything you hear in music that is produced today is compressed in some way--and it can sound anything from a subtle barely noticeable effect to a thick, dampened squish.
The benefit of a compressor lies in that every note played will be at nearly the same amplitude, and therefore nearly equal in volume. This will help normalize tones that are sometimes lost in the mix because of complex overtones, and it will result in a more articulate sound. Notice that if you don't pick all notes of an arpeggio at exactly the same pressure you will likely get a different sound for each note, especially if you are playing a tube amp. Tube amplifiers react dynamically to stronger and weaker signals--it's the allure of them--and thus the non-uniformity of picking at different strengths will be exaggerated. A compressor will fix this problem and normalize all notes of the arpeggio regardless of the player's technique and equipment, which is consequently why many soloists prefer them. Compressors also have the ability to increase the sustain of notes beyond sounds that are normally usable on the instrument; yet another reason the effect is a popular tool in the soloist's arsenal. The tiniest signal can be normalized to the same amplitude of a fierce pick attack, and a trailing note will resonate at the exact same volume until the string stops inducing a signal on the pickup.
Drawbacks of a compressor mirror their benefits. More expressive musical genres such as blues rely on the dynamics of the player. Picking technique and signal volume become part of the performance itself. In this case, a compressor will nullify exactly what you are trying to preserve. However, I have seen some blues guitarists use compressors so this is obviously not true universally.
Compressors also dampen the attack of plucking a string. This will make the note sound a bit squishy and with less snap. This is related to how fast the compressor reacts and normalizes the amplitude of the input signal. Slower reaction times result in a more natural uncompressed tone, while faster reaction times result in the trademark squishy sound. Some people consider the "squishyness" of a faster attack time a plus, while others don't like it. Nearly all compressors allow you to adjust this parameter, but it can never be completely transparent. You can get quite close with parallel compressor designs that blend the original signal into the affected signal, but the nature of the effect will still color the sound; that's what it's supposed to do.