This typically happens after an “unplanned dismount” – you get back on the bike, start riding, and realize that when the bars are pointed forward, you are pedaling in circles.
Don’t just wrench the bars back in line. Instead, loosen off the bolt(s) holding the stem to the steerer tube, line the handlebars up, and tighten the bolt(s) again.
Typically, standing over the front wheel facing backwards is the best position to do this from as you can grip the wheel with your legs while you work on making the bars straight.
There are two types of stem – quill and threadless.
Quill stems are an older design, used on threaded fork steerer tubes. They have a single adjustment bolt which is recessed into the stem and disappears inside the steerer tube. To loosen this stem, undo the bolt a couple of turns and then turn the stem back straight. You may need to tap on the Allen wrench you use to loosen the bolt in order to free the mechanism inside the stem. Be aware that the same bolt controls the height of the stem as well. If you change the height, it may change the adjustment of your front brake (depending on the design). Tighten the bolt once the stem is pointing forwards, and you are set.
Threadless stems fit on to threadless fork steerer tubes. The stem is used to hold the fork on the bike, so it does a bit more than a quill stem. For this reason, it has two sets of bolts. One – always a single bolt – is vertical, through the center of the stem cap above the steerer tube. The other set of bolts are horizontal, and act to clamp the stem on to the steerer tube. You should only have to loosen these horizontal bolts, turn the stem back straight, and then tighten the horizontal bolts again. The vertical bolt is used to adjust the tension on the whole stack of spacers, bearings and washers between the stem and the fork crown. When the horizontal bolts are tight, this vertical bolt does nothing, so don’t worry about tightening it down.
Tip: Check that your threadless headset is tight by turning the front wheel ninety degrees left or right, holding the front brake on, and then rocking the bike back and forth. If there is any rattling movement between the stem and the frame, the headset needs to be tightened up using the vertical bolt above the stem (loosen the horizontal bolts first!). Never tighten this to the point where the steering gets stiff.