When the body is invaded by a pathogen, such as a bacteria or virus, several things happen. The invading microorganism starts to multiply either within cells, or in the spaces between them, depending on the specific pathogen which is involved.
When the microorganism begins multiplying, it starts to produce proteins and other molecules which are released within cells or outside them. These non-self antigens are recognized by immune cells, thus beginning a complex cascade of events which results in an immune response that destroys the pathogen.
Of course, it’s nowhere near as simple as that. What actually happens during an immune response is far more complicated, and is dependent on several things, including the health of the immune system, the type of pathogen involved, and whether that particular pathogen has been ‘seen’ by the immune system before.
However, while other factors are involved, what happens when the immune system is activated depends largely on what type of pathogen has triggered the response. The immune response that occurs as the result of a bacterial infection is very different from the one which occurs as the result of a viral infection. The response is different again when allergies are involved (and the allergic response is an abnormal response rather than an attack against a pathogen).
Depending on the nature of the response, one or more of several types of immunological defense may be involved. Antibodies, for example, are generated in response to extracellular pathogens (those which grow outside of host cells), while cytotoxic cells are produced in response to intracellular pathogens (which grow inside host cells).