The way the body defends itself against substances it sees as harmful or foreign.