Crustaceans have hard exoskeletons which protect them from predators. As they grow, the exoskeleton can become too tight. To get rid of the exoskeleton they have to molt and climb out of the existing shell before growing a new one.
The process starts weeks before actually getting rid of the shell. Enzymes are produced and secreted in order to separate the exoskeleton. The crab also pulls and wiggles around inside the shell to encourage separation. About 24 hours before actually molting, the crab takes in a great deal of water in order to swell up and split a seam that runs along the side of the body. Once the seam has split, the crab merely crawls out. This aspect of molting takes about fifteen minutes.
Once they have shed the old shell, their soft bodies are exposed and vulnerable, though their thin proto-shell hardens up in about a week. Depending on the age of the crab, they can go anywhere from two months to three years before having to molt again. Over the course of a lifetime, a crab will molt up to 20 times.
Take a look at this amazing (yet slightly terrifying) process here: