When data packets are passing on over a network, they move through a lot of routers in their journey from the source system to the destination system. Routers work with IP packets, it means it works at the level of the IP protocol.
Every router keeps information about its other. This informative contains 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 every router.
When packets of data reach destinations at a router, its header information is checked by the router. Based on the destination and source IP addresses of the packet, the router chooses which neighbor it will forward it to. It selects the route with the least cost, and forwards the packet to the first router on that route.