A charlie horse is a cramp in a muscle. Basically, a group of muscle cells decides that they are going to contract, so, just like flexing a bicep, they all tighten up. This can be caused by lack of movement- if your leg lays in the same position for too long, or by a dietary lack, usually in potassium.
You can help prevent it by gently stretching and massaging your calves before you go to sleep and by eating a banana at bedtime.
Edit: oh, and also, drink water too. This will help your mescle cells from becoming dehydrated and contracting spontaneously.