A firewall is a network security device that monitors and controls incoming and outgoing network traffic based on predefined security rules. Its primary role is to establish a barrier between a trusted internal network and untrusted external networks, such as the internet, to prevent unauthorized access and protect against cyber threats.