A mechanical explanation, because the principle is more interesting than the technique. As always: this is for locks you own.
A pin tumbler lock has a plug (the part that turns) inside a housing, and a row of pin stacks. Each stack is a key pin and a driver pin, pushed down by a spring. The correct key lifts each stack so the gap between the two pins sits exactly at the shear line, the boundary between plug and housing. Then the plug is free to rotate.
Why does it ever work without the key? Because nothing is perfect. The holes for the pins are drilled with a tiny error, so the pin holes are not on a perfectly straight line. When you apply a slight rotational pressure to the plug, the one pin whose hole is the most misaligned touches the housing first. That stack is "binding". The others are still loose.
That is the whole mechanism: pins bind one at a time, in an order that depends on the tolerances of that particular lock. Different lock, different order.
It also explains why better locks add features: tighter tolerances, special pin shapes that react badly to uneven pressure, and key control. Nothing here is a secret, and none of it makes a door secure by itself. A door lock is a deterrent and a delay, not a vault.
A good way to learn without breaking anything: a lock of your own, opened up with the pins on the table, so you can see each stack in your hand.