The first thing to check when experiencing shifting problems is the rear derailleur, and the first item to eyeball is the alignment of the outer cog with the lower idler pulley on the derailleur.
The chain should be vertical and straight, not bent inward. But before you begin bending the cage of the derailleur to try to align it, check to see if the derailleur hanger (the tab on the frame that the derailleur bolts to) isn't bent, a victim of an off-trail adventure. The correct way to perform this check is with a tool such as those shown in the photo on the lower right. The tool is secured to the derailleur hanger and rotated; if the hanger tab is bent, there will be a larger gap between the tire and tool in one position and a smaller gap 180 degrees opposite. Mountain bikes are easier to check than road bikes, due to the fatter tires.
Slightly bent tabs can be straightened by re-installing the derailleur and inserting a long hex key into the derailleur securing bolt and carefully applying force until the tab bends and chain alignment is correct. Aluminum framed bikes with bent hangers should be left to experts as the risk of tab breakage is a real possibility. The introduction of narrow chains, Hyperglide, seven, eight and now nine speed cog sets make the importance of accurate derailleur alignment crucial to smooth shifting.
Another item to check is that the derailleur springs back smoothly after being pushed in from the side. If it is sluggish or sticky, a complete cleaning is in order. To remove the derailleur from the bike without breaking the chain, take apart the lower idler pulley, which facilitates thorough cleaning. Use an old toothbrush and a biodegradable citrus-based cleaner available from most bike shops. Coat the idler bushings with a thin film of grease, and lubricate all moving parts of the derailleur.