Gas masks filter the air around you, making it breathable. They work by using on or more of these 3 techniques.
Gas
Mask
Particle Filtration
This is the simplest form of filtration. If you place a cloth to your face to avoid breathing dust, you’ve made a particulate filter.
In gas masks that are used in biological warfare. The spaces in the mask can be extremely small.
The average bacteria is about 2 microns in size, most professional gas masks have filters which are 0.3 microns.
Over time this gets clogged, meaning you need to replace it.
Chemical absorption
Although particle filtration can stop a number of biological attacks, they can not stop chemical weapons. Vapours or mists, such as nerve toxins can pass through a particle filtration system.
In most gas masks activated charcoal or oxidizes charcoal is used to capture the toxins.
Activated charcoal is full of tiny little holes, at a molecular level. Making its surface area extremely high. As the toxin passes through the holes they are caught.
Chemical reaction
Sometimes the chemicals used in the gas aren't attracted to the charcoal and so they pass through.
In this case we have to neutralise the toxin, as it doesn't harm us.
Once chlorine gas was used in WW1, soldiers where helpless against it, until the invention of the gas mask.
Prior to the gas mask, soldiers where told to soak a rag in urine and wear it over their mouths and nose. The ammonia would neutralise the chlorine.