We have special muscular cells within our skin (in the dermis) called myocytes. They're the same that form some of our larger muscles (of the smooth or involuntary muscle type), but in this area they are isolated, and arranged in sort of a circle around the nipple.
Now, when skin muscle cells contract, they cause the skin they're attached to to "fold" because they get shorter.
In the case of nipples, the arrangement of the myocytes causes them to become hard; it's the equivalent of the goosebumps that appear in other areas of our bodies, when the dermal muscles associated with our hair contract.
Contraction of these muscle cells may be triggered by different stimuli: touch (pressure), cold (temperature), or sexual excitement ('orders' from the brain).