Routers - they connect traffic between devices that have different IP ranges
Switches - they connect traffic between devices and have independent connections between each device
Hubs - they connect traffic between devices through one shared connection (meaning their transfers may be slower than switches)
A router will recieve a packet, ask what IP it is destined for, and send it off.
A switch will recieve a packet, ask what MAC it is destined for and send it only to that one direction.
A hub will recieve a packet and send it to everything.