It is explained formally in this paper.
First the Tor Client (Alice) negotiates a symmetric key with the first relay (Bob).
Then, it uses its encrypted connection and asks the first relay to extend its circuit to another relay (Carol) and create another symmetric key for Alice and Carol.
Similarly, it can be extended to any number of relays.
A user's OP constructs circuits incrementally, negotiating a symmetric
key with each OR on the circuit, one hop at a time. To begin creating
a new circuit, the OP (call her Alice) sends a create cell to the
first node in her chosen path (call him Bob). (She chooses a new
circID C_AB not currently used on the connection from her to Bob.) The
create cell's payload contains the first half of the Diffie-Hellman
handshake (g^x), encrypted to the onion key of Bob. Bob responds with
a created cell containing g^y along with a hash of the negotiated key
K=g^(xy).
Once the circuit has been established, Alice and Bob can send one another relay cells encrypted with the negotiated key.
To extend the circuit further, Alice sends a relay extend cell to Bob, specifying the address of the next OR (call her Carol), and an encrypted g^x_2 for her. Bob copies the half-handshake into a create cell, and passes it to Carol to extend the circuit. (Bob chooses a new circID CBC not currently used on the connection between him and Carol. Alice never needs to know this circID; only Bob associates C_AB on his connection with Alice to C_BC on his connection with Carol.) When Carol responds with a created cell, Bob wraps the payload into a relay extended cell and passes it back to Alice. Now the circuit is extended to Carol, and Alice and Carol share a common key K_2 = g^(x_2*y_2).