Your client wraps a message in several layers of encryption, one for each relay on the path. The first relay removes its layer and sees only where to send it next. So does the second. The last one removes the final layer and delivers the message. No relay holds the whole picture: the first knows who you are but not where you're going, the last knows where you're going but not who you are.

The concept came out of the US Naval Research Laboratory in the mid-1990s, from Paul Syverson, Michael Reed and David Goldschlag. Tor, short for "The Onion Router," is the second-generation design Roger Dingledine and Nick Mathewson built with Syverson in the early 2000s, and it's the implementation almost everyone means today.

Onion routing protects against a local observer and against any single malicious relay. It does not protect against someone who can watch both ends of a connection and compare timing. That's the correlation attack, and it's a known, accepted limit of low-latency designs. Garlic routing in I2P is a close cousin.