A Tor client obtains the public keys of all Tor nodes just by asking their list to hosts called Tor Directory Authorities (whose public keys are hardcoded in the Tor software). Therefore, unless the Tor software was compromised, the Tor client possesses the correct public keys of all Tor nodes.
The client then decides which nodes to use in its path (circuit). As part of circuit extension request it establishes a secure TLS connection with each of the circuit nodes, one at a time starting from the first, passing through all previous nodes of the circuit. Each node is authenticated with the respective public key.
The first node has only access to the encrypted TLS traffic between the client and the second node. The first node cannot decode the traffic, hence it cannot know the data or what the third and subsequent nodes are. If the first node forwards the traffic to a node different from the one chosen by the client, the client will know because the public key will not match the one it received from the Tor Directory Authorities.