The routers form a mesh, of sorts. Each one is connected to several others (conceivably on a LAN, but typically just a direct connection, maybe over a long distance like an undersea cable or a T-carrier or fibre link), and each one knows which it is connected to. Each router typically has many IP addresses, one per link to another router it has.
The routers usually run routing protocols which allow them to discover what their neighbours (the routers to which they are directly connected) are connected to. These are IS-IS, BGP, EIGRP, RIP, OSPF, etc. They can also keep track of this statically; routing protocols are a labour-saving device that lets network administrators automate routing table maintenance in case of changes (either intentional or due to a failure). So, each router is connected to several others, and knows what is where more or less. This information can be allowed to filter through the Internet; routers will inform each other of their neighbours' neighbours also, so it is possible to build a view of everything reachable in every direction (approximately). Routers also keep track of how far away things are (how many routers are between, and potentially how fast the path is), to facilitate getting packets to their destination efficiently.
Some routers (eg. your LAN gateway, or some of your ISP's routers) have only a couple routes: the LAN they are the gateway for, and usually one default route for the rest of the internet (in IPv4 networking, this is 0.0.0.0/0, or the "default" route). They therefore know that packets going to their LAN go in one direction (out the LAN interface), and packets going elsewhere go in another direction (out the WAN interface).
Other routers with more links and more networks on either side extend this concept somewhat. They will know what prefixes (the network number part of an IP address, formally) are in each of several directions, and send packets along that route (thus their name). They relay the packet to each other this way until it reaches its destination; if this is impossible, there are several mechanisms to drop it and show that it has been dropped.
So, your packet gets to your ISP, possibly from your LAN router which knows what interface the Internet is on and sends it that way. Each successive router looks at the destination address and determines which network it is part of and so which direction it goes according to its routing table; it will know that addresses beginning with a certain number are in the "direction" of one particular other ISP, and send it that way (changing the layer 2 information at each hop in order to do this, so the MAC address for instance will change at each hop). Eventually, it reaches a router on the same network as the host you're trying to reach, and it gets to that destination.
Each of these relay steps is called a hop. You can see this work using the traceroute
utility (tracert
on windows), though you may not see all the hops due to ICMP filtering being very widespread.
1Thanks! The info I was looking for is in the first paragraph, but the for the full story too :) – Roman Bodnarchuk – 2012-11-13T16:20:13.430
Haha, this is "Network and Communications" class from school summarized in 6 paragraphs. Nicely done :) – Ярослав Рахматуллин – 2012-11-14T23:21:32.457