Tamping locks in a distribution, polishes the surface of the coffee, and eliminates any large void spaces in the coffee bed. Tamping also offers a perceptive barista feedback about dose quantity, distribution, and grind.
Contrary to popular belief, the difference in flow resistance caused by lighter and harder tamping is minimal. Once the coffee has been tamped with enough pressure to eliminate any large void spaces in the bed, additional tamping pressure will not have much effect on extraction quality or flow rate. Two factors account for this.
Very firm tamping does not seem to offer any benefits, but there are at least two reasons to tamp lightly: it causes less stress on the barista's wrist and shoulder, and it makes it easier for the barista to achieve a perfectly level tamp. (This is immediately clear when using a tamper and basket designed to have a very tight fit. When a barista tamps with a lot of force they will get stuck together much more frequently, indicating the tamper is not level.)
A recent point of contention in the tamping debate is whether to tap the side of the portafilter between tamps. The argument in favor of tapping is that it dislodges any loose grounds which had crept up the walls of the basket during the first tamp, and those grounds can then be sealed into the coffee bed with a second tamp. It is hard to see how incorporating a few loose grounds into the coffee bed is worth the potential harm done by tapping. The tap can break the seal between the grounds and the wall of the basket, creating an easily exploitable channel around the edges of the coffee bed. In my experience a broken seal is difficult, if not impossible, to fix with a second tamp. It might be possible to tap without breaking the seal, but tapping does not seem worth the risk. The bottom line: a few loose grounds are a minor problem, if in fact they are a problem at all. (I don't think they are.) A broken seal between the grounds and the basket is a major problem. One barista I admire taps with her wrist (an action akin to a strike with a "dead blow" hammer) in order to limit any jarring of the coffee bed. If you must tap, this seems to be a safer method than tapping with the hard handle of a tamper.
How to Tamp