With the crescent moon in his hair, Shiva is called Chandrashekhara.
After Sati had committed suicide, Shiva withdrew into the woods and went into asceticism. This asceticism was so powerful that everything around burned up. As soon as Shiva wandered on, everything around him burned again. Soon the entire earth would be burned.
The gods asked Brahma to take care. Brahma made Shiva Chandra, the moon, a gift. Shiva pinned him in his hair and ended the asceticism.