A broken key inside a door lock requires immediate attention, and can sometimes be fixed quickly by inserting a pair of needle-nose pliers into the keyhole and pulling out the end of the key. If this doesn't work you'll have to disassemble the lock and push the key out of the hole. Breaking a key inside a lock will permanently damage the lock in some cases.
Deadbolt locks are the most common type associated with breaking a key, as they are harder to turn due to the weight of the bolt. Remove the trim screws from the lock face with a Phillips screwdriver and pry the lock trim away from the door. Press the tip of a flat head screwdriver into tip of the key and out through the keyhole. Spray a bit of silicone-based lubricant into the lock interior and reassemble.
Keyed Knob Lock
Remove all trim screws along the door edge and knob with a Phillips screwdriver. Pull the edge plate away from the door edge and pull the knobs apart. Insert a flat head screwdriver into the end of the spindle, hold the knob in your other hand and turn the screwdriver counterclockwise until it stops. Slide the lock cylinder out of the knob and push the key out of the cylinder with the flat head screwdriver. Lubricate and reassemble the lock.
A mortise lock consists of a large lock mechanism set inside the door edge, with the keyhole located beneath the knob. Remove all trim screws along the door edge and lock facing with a screwdriver, then gently pry the lock casing from the door edge with a flat head screwdriver. Disassemble the lock casing and press the blade of the flat screwdriver against the back of the key to push it out of the keyhole. Lubricate and reassemble the lock.
Lock Maintenance & Prevention
A broken key in a lock often indicates that the lock is in need of maintenance. If the key was weak or broke off due to force it's probably not a maintenance issue, but if you noticed that the key was turning hard or grinding before it got stuck you'll need to lubricate the lock on a more regular basis. Apply a silicone-based lubricant spray to the keyhole every six months to keep the lock in good working order and prevent keys from breaking.