A firewall protects networks and computers from unauthorized access. There are two main types - software firewalls that protect individual computers, and hardware firewalls that protect entire networks. A firewall works by inspecting all incoming and outgoing data packets and determining whether to allow or block them based on a set of rules. Firewalls can block hackers, enforce security policies to protect private information, and log internet activity. However, firewalls cannot protect against insider threats, connections not routed through the firewall, or completely new viruses.