The series of images below explain how the park mechanism on your car keeps it from rolling.
Figure 1 shows the output section of the transmission. The parking brake mechanism engages the teeth on the output to hold the car still. This is the section of the transmission that hooks up to the driveshaft. If this part can't spin, the car can't move.
Figure 2 shows the park mechanism protruding into the housing where the gears are located. Notice that it has tapered sides. This helps to disengage the parking brake when you are parked on a hill. The force from the weight of the car helps to push the parking mechanism out of place because of the angle of the taper.
Figure 3 shows the rod that actuates the park mechanism. This rod is connected to a cable that is operated by the shift lever in your car.
Figure 4 shows the park mechanism from the top. When you place the shift lever in park, the rod pushes the spring against the small tapered bushing. If the park mechanism is lined up so that it can drop into one of the notches in the output gear section, the tapered bushing will push the mechanism down. If the mechanism is lined up on one of the high spots on the output, then the spring will push on the tapered bushing, but the lever will not lock into place until the car rolls a little and the teeth line up properly. This is why sometimes your car moves a little bit after you put it in park and release the brake pedal. It has to roll a little for the teeth to line up to where the parking mechanism can drop into place.
Once the car is safely in park, the bushing holds down the lever so that the car will not pop out of park if it is on a hill.
For more articles on brake types and braking, check out the links below.