NOTE: DNS resolution occurs before any of the below in order to determine destination IP address.
If the request IP Address is on your local LAN, your router doesn't even get involved (aside from the switch part which is a series of ethernet ports).
Your local routing table (command line route print
on win, plain route
on lin) shows you the routing table that the OS uses to figure out what interface to send the packet out of depending on addressing. The IP stack will then do an arp request to find the MAC for the machine on the local network and establish direct communications through the switch.
If the IP address is foreign to your local network, then the OS routing table sends it to the gateway address, which is your router and your router uses a similar routing table to forward the packet to its gateway, ad nauseum till it gets to destination.
1
For the long and ugly detail see: http://serverfault.com/questions/49765/how-does-ipv4-subnetting-work
– Zoredache – 2013-07-25T00:05:01.520