When you shift your manual transmission into gear, a collar with "dog teeth" on it engages the gear. A shaft from the engine turns the layshaft, which turns that engaged gear. Then the gear passes its energy via the collar to the drive shaft. Other gears freewheel until they're engaged.

Double-clutching was a common requirement in old cars and some race cars still require double-clutching. To double-clutch shift, first you press the clutch pedal to free the engine from the transmission. This allows the collar to move into neutral without the engaged dog teeth in the side of the gear. When you release the clutch pedal, you have to rev the engine to get it to the right rpm value for the next gear. This means you want to get the collar and the next gear spinning at the same rate so that the dog teeth can engage the gear. Once the engine hits the right speed, you depress the clutch pedal again in order to lock the collar into place on the next gear. When you're working with a double-clutch, you need to press the clutch and release it twice any time you want to switch gears.

When you move the gear shift knob, it changes the gears by moving a rod that's connected to a fork. That fork moves the collar toward whichever gear you want to engage next. When you mess up your shifting and hear a terrible-sounding grinding noise, it's not the teeth missing each other. As previously noted, the gears are always meshed properly -- they're just not always engaged. The grinding noise is actually the sound of the dog teeth on the collar missing the holes on the side of the gear.