For most animals, defence against predators is vital. Being eaten is not the only threat to life: parasites and diseases may also be fatal. But animals, especially small animals, are often eaten. 

Since life is about reproduction, anything that keeps a creature alive long enough for it to reproduce will be selected for. The next generation comes from the fertilised eggs of the previous generation.

Not surprisingly, then, animals have developed a wide range of defences against predation. Some of these defences apply also to plants: herbivory on plants can be very damaging, and reduce a plant's fertility. Many plants have some defence against herbivores.

Primary defences 
Primary defences make it less likely that there will be a meeting between the predator and the prey. In many cases this means the predator has not noticed or recognised the prey.

Definitions 
There are various definitions of what a primary defence is:
Robinson 1969: those defences which operate before a predator starts any prey-catching behaviour.
Kruik 1972 and Edmunds 1974: defences which operate whether or not a predator is nearby.

Methods

For palatable prey 
If the prey is good to eat, it must either make itself scarce, or it must look as though it is not good to eat. A third possibility is that the prey is too small to bother with. In other words, the effort of catching and eating it would not be worth the effort expended by the predator.
Hiding away (anachoresis): many small animals live in holes or crevices. This way they cannot be seen unless the predator searches carefully, which some birds do. Also, sooner or later, the hidden animal must come out into the open. But while hidden, it is relatively safe. Very common is for the prey to hide during the part of the day its main predator is active.
Crypsis: this is the general term for animals which use camouflage to resemble their background. They are visible, but usually not seen (noticed).
Batesian mimicry: a tasty animal pretends it is foul-tasting by resenbling warning colouration.

For prey with defences 
Defences may be of two kinds. Either the prey is uneatable because it has some kind of chemical defence, or its body is protected by spines or stings. In that case the predator will reject it so long as the prey advertises its defence.
Warning colouration (aposematism): the opposite of camouflage. The animal signals to predators that it is unpleasant to eat.
Warning sounds: Sometimes the same principle occurs with senses other than sight. Warning sounds allow the animal to stay hidden. Rattlesnakes are a good example: they need crypsis to avoid alerting their prey, and use the rattle to avoid being accidentally injured by large animals. Moths of the Arctiidae and Ctenuchidae families are foul-tasting, but might be eaten by bats at night. These moths emit clicks when they hear bats in flight. That the clicks work is shown by a neat experiment. Bats eat mealworms if thrown up by the experimenter. They ignore the mealworms thrown by hand if moth clicks are played at the same time.
The second kind of defence is very obvious. It is to carry armour and/or offensive weapons like horns. These are permanent visible defences. For larger prey animals this can be effective if linked to another defence. Thus, in natural habitats, elephants have tusks, and they also have huge size. Antelope have horns, and can also run fast. Almost all large herbivores live in herds, which undoubtedly is safer than living alone.

Secondary defences 

These work when a predator detects a prey, or when a prey notices a predator. The function of secondary defences is to increase the chance of the prey surviving the encounter (meeting).

Methods 

Withdraw to a prepared retreat: many methods. The pearl-fish Carapus apis retreats backwards into the anus, and then into the body cavity of a sea cucumber (Holothuria).p138 It turns out that the fish, when a juvenile, is a parasite on the sea cucumber. Although it is free-living as an adult, it is still able to re-enter the holothurian when danger threatens. Better-known examples include the tortoise, which just withdraws its head and legs under its shell, and the hermit crab, which retreats into its shell when in danger.
Flee: run or fly away: a popular plan. Animals may flee in a straight line, or zig-zag. Another method is to fly showing some flash colour, and then suddenly settle completely still in camouflage. There are a number of moths with bright hind wings which are covered when at rest.
Bluff: look threatening or startling (deimatic behaviour). The prey may adopt a posture which causes the predator to pause. This is sometimes called a 'startle display'. The predator may not know whether the display is  a genuine threat or a bluff. Either way, the prey has improved its chance of escape.
Play dead (thanatosis): many vertebrate predators reject dead animals. Many predators strike to kill only on prey which move. That includes cats, lizards and mantids. A number of insects play dead when attacked. Some larger animals also do this, like the American Opossum. What playing dead does is avoid releasing the predator's inherited killing behaviour.
Deflect or distract the attack. Many offer a non-essential part of the body, for example, butterflies whose wings have small 'targets' near the edges. Cuttlefish squirt out a cloud of black ink, turn pale and swim fast away from the ink. The Kentish plover, which lays its eggs on the ground, lures attackers away from the eggs. It uses a remarkable impression of being damaged with a broken wing. When the predator is well away from the eggs, the bird flies off.p175 Other plovers sit on imaginary nest sites: both methods are distraction displays. Quite a number of animals can break off a non-essential part of their body, and may regenerate it later.p179
Fight back (retaliation or fighting defence): there are a very large number of animal weapons, and many are used for face-to-face fighting. Bombardier beetles squirt hot poisonous liquid at attackers. Large herbivores often carry weapons: Triceratops, Iguanodon, elephants, deer, Rhinoceros, Hippopotamus. Almost always the weapons are used for several functions, not just fighting predators.
Band together: many animals use defensive groups against predators. Very common in birds and mammals.

Costs of defence 
Everything costs something. All defences incur costs. For survival, an animals needs food, and needs to stay alive to reproduce. Energy and time spent on defence can be considerable, but justified if it makes reproduction more likely. There are these kinds of costs:
Internal costs: protection given by some chemicals may slow growth
Opportunity costs: hiding reduces feeding
Environmental costs: best places for defence may not be best for life functions
Design and self-damage costs: body shape or colour for crypsis may not be best for mate recognition.

Related pages 
Defence against herbivory

References

Other sources 
 

Coevolution
Ecology