A switch uses a mac-table where data is routed only to the intended recipient via it's Mac address; it keeps a table correlating the switch port to mac-address. A hub is open to all devices on it. Hubs are prone to being sniffed, while switches are more secure, but can be sniffed with "mac flooding".
A hub is typically the less expensive, less intelligent, and less complicated. Its job is very simple: anything that comes in one port is sent out to the others. That's it. Every computer connected to the hub "sees" everything that every other computer on the hub sees. The hub itself is blissfully ignorant of the data being transmitted. For years, simple hubs have been quick and easy ways to connect computers in small networks.
A switch does essentially what a hub does but more efficiently. By paying attention to the traffic that comes across it, it can "learn" where particular addresses are. For example, if it sees traffic from machine A coming in on port 2, it now knows that machine A is connected to that port and that traffic to machine A needs to only be sent to that port and not any of the others. The net result of using a switch over a hub is that most of the network traffic only goes where it needs to rather than to every port. On busy networks this can make the network significantly faster.