Vaccines have been around for a long time, but the immune system hasn’t really been understood until very recently. For example, smallpox vaccine is only effective with an “adjuvant”, nobody understood why, theory is it is needed to activate the immune system to treat it as a threat.
The immune system has the difficult task of figuring out when to activate: it should only attack harmful bacteria, and not your body’s cells and not harmless particles. Allergies and autoimmune diseases occur when these fail. Innate immunity is hardcoded into our DNA: receptors for specific bacteria that is hard for the bacteria to evolve resistance, like mitosis.
Otherwise, the adaptive immune system can deal with more types of infections. Dendritic cells sound the alarm and bring an example of the pathogen to a lymph node, perhaps by detecting that cell damage has occurred. During first stages of immune response, T-cells that match the pathogen multiply: they’re created with a random assortment of receptors and only the ones that target the pathogen are allowed to multiply.
Immune cells communicate using proteins called cytokines, some important ones include interferon and TNF. Immunotherapy deals with activating and blocking various cytokines to indirectly treat a disease by controlling the immune system, and has been successful in treating rheumatoid arthritis (an autoimmune disease) and some types of cancer. However, these therapies are still crude, only work with some patients, and often cause side effects because we don’t understand the immune system well enough. A lot has only been discovered in the last few decades, and more research will help us develop better medicine.