Firewalls work by filtering packets of data that are trying to enter or leave a network. They can be configured to block data from suspicious IP addresses or restrict access to certain websites. This filtering can be based on various criteria like IP addresses, domain names, protocols, programs, and ports.