Routing
Routing tables, longest-prefix match, next hops, and how the internet is stitched together from autonomous systems with BGP.
Every host and every router answers the same question for every packet — "given this destination address, which interface and which neighbour?" — by looking the address up in a routing table and taking the most specific match.
Given `10.0.0.0/8 → A`, `10.10.0.0/16 → B` and `0.0.0.0/0 → gateway`, the destination `10.10.42.7` goes to B because sixteen of its leading bits match that entry and only eight match the other — and this rule is what lets a router summarise the world into one entry and still carve out exceptions.
The internet is tens of thousands of independently run networks that tell each other which prefixes they can reach and by what path, and choose among those paths by commercial policy rather than by distance — which is why your packets take the route they take and why the return path is usually different.