Tonsils and adenoids are strategically located near the entrance to the breathing passages where they can catch incoming infections. They 'sample" bacteria and viruses and can become infected themselves. It is thought that they then help form antibodies to those "germs" as part of the body's immune system to resist and fight future infections.
This function is performed in the first few years of life, but it is less important as the child gets older. In fact, there is no evidence that tonsils or adenoids are important after the age of three.