What causes muscle spasms (leg cramps, etc.) ? What practical things can you do to help prevent them?

What causes muscle spasms (leg cramps, etc.) ? What practical things can you do to help prevent them?