Beside each gear is a synchronizer hub that is attached to the mainshaft by internal splines. When you shift into a gear the shift fork slides a synchronizer sleeve or collar toward the selected gear and over the synchronizer ring and clutch. As you do this the synchronizer hub pushes onto a clutch, which then slows the spinning gear until it matches the speed of the spinning synchronizer hub. When the speeds match, dog gears on the hub and gear are aligned and the collar locks them both together, and power can now be transmitted through that gear to the main shaft. When the dog gears are not aligned they make the horrible noise known as grinding.