If the body is too cold, the hairs are raised by small muscles to trap a layer of air near the skin. Since, air is an insulator it keeps the heat in.
If the body is too warm, the hairs of the body lie flat to avoid trapping an insulating layer of air.