How a Router WorksWhen data packets are transmitted over a network (say the Internet), they move through many routers (because they pass through many networks) in their journey from the source machine to the destination machine. Routers work with IP packets, meaning that it works at the level of the IP protocol.
Each router keeps information about its neighbors (other routers in the same or other networks). This information includes the IP address and the cost, which is in terms of time, delay and other network considerations. This information is kept in a routing table, found in all routers.
When a packet of data arrives at a router, its header information is scrutinized by the router. Based on the destination and source IP addresses of the packet, the router decides which neighbor it will forward it to. It chooses the route with the least cost, and forwards the packet to the first router on that route.