A router can be basically defined as
Or more precisely as
And that's it! A router is a device that can connect two dissimilar networks (like your home network and the internet) and allow for traffic to pass between them.
So...how does a router work?
To understand this you've got to know about two things. IP Addresses, and Subnets
An IP address is a uniquely identifying address given to a network device. IPs are generally written in "dotted" notation which is the standard recognizable IP format. This notation involves separating 4 groups of 3 digit numbers with a dot (like 192.168.0.1). These numbers are the "real number" representations of binary "octets" or groups of 8 bits. This means that each individual group of numbers in the IP address conveys 8 bits of binary information.
A subnet is a "block" or range of IP addresses that define a specific contiguous network. You will usually see this represented as a "Subnet mask" that you assign to a network device (the most common one seen is 255.255.255.0). A network device uses this subnet mask to separate the network portion of the IP addresses that it sees and the host portion of the IP addresses. You'll notice that a subnet mask is in dotted notation just like an IP address, and a subnet mask works the same way. It's 4 groups of 8 binary bits. When a device processes a subnet mask it will translate the mask into a binary number that it can use to strip the network portion off of the front of the IP address leaving the host portion behind.
Once you have a network portion and host portion of an IP address you can finally know which computer the data is destined for and on which network. So with an IP address of 192.168.0.2 with a subnet mask of 255.255.255.0 the device would see 192.168.0 as the network portion and the .2 as the host portion, which means that the device in question has a "host id" of .2 and belongs to the 192.168.0.0 network (don't have time to teach you how that happens...but by god it happens)
A good way to understand the basics of IP addressing and Subnetting is to think about how normal "snail mail" is delivered in a town. Each home in the town is on a named street, and each home has a number assigned to it. You can visualize the "host" portion of an IP address by thinking of it as your home's address number. This is a number that's specific to your home and allows the mailman to deliver letters to your home. The "network" portion of the IP address can be visualized as your street name which allows your mailman to know what part of town your home is located in.
Another way to visualize IP Addresses within the context of subnets and routing is to think of a corporate phone system. The company will have a main number that you dial into to connect to their phone system, then within that system each person within the company has their own personal extension that allows you to get to the right person within the company. In this analogy, the main number for the company would be the broad subnet and each individual extension would correspond to the host portion of the subnet
So what does all this have to do with routing?
Well it's logical to assume that there are homes in your town that share the same address number, but there should be no other house in your town that has your same house number AND is on the same street as you. This is how IP addresses and subnet masks work, they allow a networking device to determine which network (street name) a device resides on, and then to ensure that the information arrives at the specific device (home address number) on that network that it was destined for.
A router is like the post office. It knows which street is where and which house is on which street. When a letter comes into the post office, it has an address on it that contains the recipient’s street name and house number. The post office then sorts the mail it receives by this info and hands the mail to the mailman who delivers to that street.
This is the same way a router works. A router will get a packet of information from your computer and strip off the network address and host address from the packet and decide where to route (wonder why they call it a router) the traffic to.
So why do we need routers? Can't my computer just send the info directly?
Well, no, your computer can't do this directly. Because of IP addresses and subnet masks, your computer only knows about things that exist on its own subnet and therefore can only communicate directly with devices on that subnet. That's where routing comes in. A router can also be known as a gateway (think default gateway). A gateway is a portal from one location to another. And that's what a router is, a portal from one subnet to another. The router will have known "Routes" for different subnets. These routes can be manually or dynamically assigned in various ways and through various protocols (Google routing protocols). These routes are like road maps that tell your router where to send traffic. Most routes in a router actually point to a different router on a different subnet. If you ever look at your route tables in your router you'll see a route that's similar to 0.0.0.0 with a netmask of 0.0.0.0 pointing to another IP address (that address is usually going to be your internal router, but if you look at the route tables on your modem, then that IP address would be your ISPs router). This is known as a default route. This route tells your router to forward any unknown traffic on to the next router who probably knows the route to the final destination. By using routing tables in this way your network can connect to a network with a different IP structure and establish communication.
Let's put this into action
So let's assume that we've got two devices. One is a pc in your home (IP address: 192.168.0.2 Subnet Mask: 255.255.255.0) and one is a server out on the internet (IP address: 192.168.222.3 Subnet Mask: 255.255.255.0). How does information get from that server to your computer when you request info from this webpage with your PC?
First, your computer issues the request to its default gateway (your home router) and asks if the gateway knows how to get to the destination. Your home router doesn't know how to get to this destination but it knows that your modem (your router's default gateway) probably does so it sends the packet on with a request to your modem. Chances are your modem doesn't have a clue how to get to the destination either so your modem forwards the request on to the ISP's router. This process continues until the packet finally reaches the destination at which point the destination server will process your request and initiate the transaction in reverse until the packet gets back to your home PC which then displays the webpage you were looking for.
Keep in mind that this is a very bare bones explanation of how this works and the actual process would take me a few hours and pages to really explain.