Firewalls act as a gatekeeper, filtering incoming and outgoing network traffic based on established security rules to block unauthorized access.