No-one actually knows why. The theory is that they loose their sense of direction, swim into shallow water, and get beached as the tide goes out. The problem is, a whale's lungs will not support it's body weight without water buoyancy, so the suffocate themselves fairly quickly unless they can refloat on the next tide, by when it's usually too late, they either suffocate, or die of shock.
They don't choose to! Usually for whatever reason like Andy says it's because once they get stranded in shallow waters they can't get back in the sea themselves...
I've read that it might be due to temporary irregularities in the earth's magnetic field but that sounds a little bit too much like Mr Spock to me. so, actually I don't have a clue.