Have you ever tried to pick a lock like in the movies with no luck? Well, lets learn about the inner workings of a lock to see how this works.
When you put a key in a lock and twist, you turn a part called the cylinder. The cylinder then turns a part called the cam, which makes the bolt slide in or out, depending on which way you turn.
Inside a lock are several shafts running through the cylinder and into the casing around the cylinder. Inside these shafts are pins of various lengths. Without a key inside, the bottom half of each pin rests inside the cylinder, while the top of the pin extends beyond the cylinder to the surrounding casing. This is what holds the cylinder in place. If you try to twist the cylinder without correctly moving the pins, the top pins will catch on the casing.
This is where the correct key comes in.
When you insert the right key, its notches raise the pins just enough so that the bottoms of the pins are aligned with the space between cylinder and casing. Since none of the pins are in both the cylinder and the casing at the same time, the cylinder turns freely.
Thus, when you pick a lock, you're using a paper clip or whatever to push the pins up like the correct key would.