Can someone explain how IP routing works to me?
E.g. if i ping 18.104.22.168, how does each router know which way to go as shown in a trace route dump.
This explanation will be simplified let me know if you want more details. It's not completely accurate but it should give you a a good explanation.
It basically bubbles up. When you do ping x. If your computer doesn't have address x in it's routing table it goes to it's gateway (if you look at your routing table it will be the one that has destination default or 0.0.0.0) then it will look at it's routing table to see if it has a route that matches this address or ip range if for example if it had 22.214.171.124/24 as a route it would send it to what ever ip address it has associated with that route. If it doesn't it will do the same as your computer, send it to the default route.
For your computer for example, this will end when your ISP receives this and it will send it to what ever peer it knows has a route for this address (or is likely too, they share routing tables at that level). Then the reverse process happens, it goes down towards more specific ranges until it reaches the computer in question.
The commands route and traceroute(tracert for windows) might help make this clearer. On unix these commands have a -n option that make them show the address and not the dns name of the particular address.
using route, my default route is show do be:
the IFACE here is important because it tells the computer what network card is connected to this router which is at 192.168.1.1. My computer will send all packets with unknown location through the ETH0 network card.
Each router gets an IP packet with a destination address. It will pick a next hop, based on the most specific route that matches the destination.
If we start from your computer, it will have two (there may be more, but "two" is the typical case) routes installed. One is for the LAN it's on, the other is for 0.0.0.0/0 ("the internet", usually called the default route). If the machine you ping is not local to the LAN, the packet will be sent to the default gateway.
Then, each intermediate router, until the router that has the destination LAN attached, will do the same thing. Look up the most specific route to the destination, then send teh packet on.
The intermediate routers will, most probably, have learned of the best route using one or more routing protocols (BGP, OSPF or similar), both to lessen the administrative burden and to provide a measure of fault tolerance.