Difference Between a Router and a Switch
A router a switch and a hub are all networking devices which move data from one device to another. There are very important differences between how they do this. A hub is not able to perform the same tasks as a router.
Hubs are stupid OSI layer 1 devices. You can think of a hub as a repeater. Whatever data is sent into one port of a hub is announced out of all the other ports of the hub. This leads to a single collision and broadcast domain.
Hubs are fairly rare these days. Switches have become cheap enough that almost no one uses a hub.
Switches are generally layer 2 devices. They are concerned with frames. If a switch knows the MAC address of the device it is sending data to, it will only send out of that single port. This prevents collisions. A switch breaks up each of its ports into separate collision domains.
However, a switch is still a single network, and there is a single broadcast domain. When data is sent as a broadcast it goes out all the ports of the switch.
A switch does not know anything about IP addresses or anything above layer 2. It doesn't understand IP or TCP information.
If you have a network which has multiple devices all talking to each other, you are probably using a switch.
A router is a OSI layer 3 device. It passes packets and usually uses the IP protocol (IPv4 and IPv6). It is the most advanced of the three device types. Every port on a router is its own broadcast domain. Because of this routers are able to have each of their ports belong to a different network. Port A can be network 10.10.10.0/24 and Port B can be network 18.104.22.168/24. A switch and hub cannot do that.
Routers are necessary when running any type of routing protocol, such as OSPF or BGP. This is because routing protocols are concerned with layer 3 packets and rules. Anytime you need to do something IP specific you need a router.
If you have a device which allows you to talk to other networks, such as the Internet, then you are using a router.
A router is smarter than a switch, which is smarter than a hub. Switches work inside a single network connecting th devices of that network together. A router connects different networks together. Many home ISPs issue users a router which also has the capabilities of a switch. It connects all the local computers into a network and also allows communication between the local network and other networks.