We can understand firewalls as networked security devices. The central purpose of a firewall device is to inspect both incoming and outgoing traffic and decide which to block according to a particular set of rules or policies.
Firewalls can operate in a stateless or stateful manner. Stateless firewalls do not keep any information regarding the connections between clients and servers, inspecting each packet independently. Stateful firewalls, in turn, keep track of the connections, thus monitoring their stages and identifying unusual and potentially malicious behavior on them.