Bike handlebars come in many styles. There are straighter ones on mountain and street bikes, upright ones on BMX-style bikes, and drop bars on road and track bikes. All styles fit into a piece of metal called the "gooseneck" or "stem," which connects the curved handlebar piece to the vertical "steering tube," which connects to the front fork. The two principle points of adjustment are the same though: the rotation around the horizontal axis is adjusted at the front end of the stem, while the rotation around the vertical axis is adjusted at the back end of the stem.
Determine which of the two ends of the gooseneck or stem is loose. If the handlebars are rotating around a horizontal line, the front end of the stem needs tightening. If you can move the handlebars around a vertical axis so that they're not always pointing straight ahead with the front wheel, then it's the back of the stem that needs tightening.
Determine the tools needed. For example, some bolts have a hexagonal hole in which the wrench is inserted. These are called Allen screws. They use less material than regular screws and are, therefore, used for lighter, high-performance bikes. These types of screws require an Allen wrench. Other stems still have the more traditional hex head screws, which take a standard crescent wrench.
Tighten the front of the stem as follows. Rotate the handlebars to the position around the horizontal axis that will make you comfortable while riding. Then tighten away at the screw(s) at the front of the stem. The front of many stems nowadays has four screws instead of just one. You should tighten each a little at a time, giving each one a little turn before going to the next one. Keep rotating between them until they are each very tight.
Tighten the back of the stem as follows. Rotate the handlebars to be centred with the front wheel. Then tighten the bolts that clamp the horizontal stem to a vertical piece. (A gooseneck needs the single bolt on top of its bend to be tightened. Modern stems split up the function of the gooseneck's single bolt into multiple bolts.)
A gooseneck has a sharp bend in it, while a stem is a straight piece. On the gooseneck, the bolt for orienting the bars with respect to the front wheel is located on top of the sharp bend.
If you tighten the screw on top of the back of the stem/gooseneck, and the screw won't "catch" on anything in the steering column, turn the bike upside down and pull out the wedge inside. Screw it back onto the bolt before reinserting.