Your body doesn't simply sit idly by as viruses come in and kill off all your cells. For millions of years, we have been exposed to viruses, and have developed various ways of defending ourselves against virus infections, since punching viruses never works. This may not seem obvious, but viruses don't infect us with the goal of killing us or causing us disease. Many of the symptoms we see in a virus infection – runny nose, fever, skin redness – are a function of the immune response to virus infection. So, the immune system is a double-edged sword. We need it to protect ourselves from virus infections, but it makes us miserable when it works. However, viruses have evolved so that some symptoms of diseases – sneezing, diarrhea – actually help virus spread from host to host.
The immune system is far too complicated to stick in one section on a chapter on viruses, but we will talk about the arm of the immune system that handles virus infection. There are two types of immunity: innate and adaptive immunity. Innate immunity is basically the first line of defense of a host against an infectious agent. Because it is so basic, it includes such minimal defenses as the skin and mucous membranes, proteins that non-specifically degrade foreign cells, and a giant "Beware of Dog" sign. Obviously, this defense is not well tailored to stopping a virus infection.
On the other hand, the adaptive immune system is a branch of immunity evolved in mammals that is like "smart bomb" technology. We are exposed to a pathogen that makes us sick, then our adaptive immunity "remembers" this, so the next time we see that pathogen, it disposes of it like week old fishsticks.
The adaptive immunity has two important cells: B and T cells (B means cells that mature in the bone marrow, while T cells develop in the thymus). B cells make antibodies, and are not as useful in defending against virus infection as T cells. T cells are highly important in the immune response to virus infection because they do a few things:
The major defense against virus-infected cells is the CD8+ T-cell, also called cytotoxic T cell (CTL), killing of virus-infected cells. In immunology, all proteins are called "CD" followed a number, because immunologists have no imagination, and they never got on board with the whole "mp3" phenomenon. So, CD8 is just a protein that is found on the surface of certain T cells that kill infected cells (either virus, bacteria, or parasite).
Activation of T Cells (Source)
How do CTL cells recognize that a cell is infected? All T cells have a T-cell receptor (TCR in Figure 1), which recognizes a protein complex in infected cells, also called antigen-presenting cells, called MHC. MHC complexes continuously display short peptides of proteins in the cell. And if a cell is infected with a virus, then many of the peptides will be virus peptides. The T cell sees these virus peptides and says "WHAT IS THIS????" and then kills that cell. This is why we can't take CTLs to Europe. Croissants just freak them out.
CTLs kill infected cells by activating apoptosis in the infected cell. Apoptosis is programmed cell death, sort of like cell euthanasia. Other cells, such as natural killer (NK) cells, can be activated by T cells to kill infected cells. NK cells work similar to CTLs, but they don't respond to virus antigens, like CTLs do. So, they are more a part of innate immunity than adaptive immunity, even though they are activated by T cells.
The cool thing about adaptive immunity is that once a virus infection has been controlled, the cells that were responsible for clearing the virus are rewarded, and more cells like those virus-killing cells are made. It's like CTLs are treated as war heroes, and everyone wants to be like them. The best part about this is that next time that virus tries to get into a cell there are several more types of cells that are ready to kill that specific virus. This is an important fact that we'll come back to in the vaccines section.
T cells can also inhibit virus replication by activating cells to make a compound called interferon, which does what the name implies: interfere with virus replication. Interferon is mostly responsible for activating two genes, protein kinase R (PKR) and oligo-A-synthetase (OAS). Interferon activates several other genes that have antiviral function, but PKR and OAS are the most significant as they completely shutoff protein synthesis in an infected cell.