The operating system IP stack works out where to send it as follows.
First you need to understand some basic addressing. This won't be very thorough but hopefully good enough for you to make some sense of it.
For IPv4 you have a network address and a host address.
So for an address of 192.168.1.10 with subnet mask 255.255.255.0 you have the first 3 bytes used to address the network. i.e. the 255.255.255 part. The 0 bit at the end represents the host part of the address. Actually you can also have subnet masks of 255.255.255.240. But, to keep it simple I'll just say that these numbers are used to determine how the 4 bytes or octets that make up the address are split between network and hosts.
In the case of a subnet mask 255.255.255.0, the 0 part at the end means we can have 255 hosts.
Now for the routing part.
If we're sending a packet to 192.168.1.11 then the IP stack will know it's a local address and just send it directly addressed to 192.168.1.11 because the first 3 octets match my local network. However, if I was to send a packet to 1.1.1.1 then it would be sent to whatever is defined as the default route in my operating system. The default route is a router that will handle the "I want to send a packet to 1.1.1.1 for me". So anything that isn't local that doesn't begin with 192.168.1 will be sent to the default router. How the router knows how to reach 1.1.1.1 is what routing protocols are all about. But you simply asked how the packets get "off network" so I hope this answer explains it well enough for you.
11 is a good description of what happens at the IP level. 2 is a pretty good description of what happens at the Ethernet level, except the switch probably does know the router's hardware address and so doesn't have to send them to all connected devices. The packet your PC sends has an IP destination address of 1.1.1.1 and an Ethernet hardware destination address of your router's Ethernet hardware address. – David Schwartz – 2012-08-02T21:14:16.337