A key extractor is a tool made of a very thin steel blade with small teeth or a hook at the tip, designed to remove the piece of key that stays inside the lock cylinder when a key snaps. It is slim enough to slide in alongside the broken piece without damaging the pins.
Locksmiths carry several, of different thicknesses and shapes, because every cylinder and every break calls for a different one. They are often used together with an endoscope, to see how the piece has lodged before hooking it.
Improvising one with a hairpin, a needle or a hacksaw blade usually makes things worse: the piece gets wedged further in and you may end up needing a new cylinder. If your key has snapped, do not put anything in and see broken key extraction.