Think of your ratchet hand tool. On that one you can change the drive direction via a lever. If you then installed a motor on your ratchet the movement of the motor could be used to reposition the lever allowing the motor to drive the ratchet in both directions. A return spring on the lever could then be used to lock the ratchet in the direction you desire.
That's the principle of how it works.