Vaccines are proteins (antigens) that initiate a protective immune response within an animal. In most cases, the immune response involves antibodies that can block either the disease agent (bacteria or virus) or its product (toxin) so disease does not occur.
The first time an animal is exposed to an antigen, the response is slow and may not be strong. This is why in natural infection animals still develop disease and why many recover over time. The disease triggers an immune response that includes the production of antibodies that reduce or nullify the disease.
However, if the disease is severe, death or production losses will already have happened as the immune response is occurring. If an animal does survive, invariably the next time they are faced with the same challenge, their immune system is primed and ready, and the antibody response is much quicker and stronger – resulting in the disease having little or no effect. This is why most animals are considered ‘immune’ to a disease after they have had it.
Vaccination aims to generate this immune response and make animals immune without (in general) giving them the disease or experiencing its effects.