A firewall acts as a security gateway between trusted and untrusted networks like the internal network and the internet. It tracks and controls communications, deciding whether to pass, reject, encrypt or log traffic based on access control rules. Firewalls provide perimeter defense and impose restrictions on network services to only allow authorized traffic, auditing access and protecting data integrity. They can be implemented as standalone hardware devices or software on client computers and servers.