Some very diverse answers on here, including some very humorous ones.
I'm no expert in the field, but I understand the earth rotates because it always has done since lots of particles swirling about a central point began to clump together because gravity attracted them to each other. As someone else said, it is slowing down very gradually but, because there is no air resistance in space there is nothing to stop it rotating, although I suppose that gravitational attraction of and to other celestial bodies must have some effect.
So it is not gravity or a magnetic field that causes it to rotate.
On the other hand, gravity influences the earth's orbit of the sun. Again, it has been doing this since it first formed, probably as particles which were attracted to the sun by its gravity but achieved an orbit rather than plummeting directly into the sun. Like its rotation, I suppose the earth's orbit of the sun must be very gradually slowing down. If not, we must have achieved 'perpetual motion' which, I understand, is not possible!
It seems that just about everything in the universe rotates and orbits in these circular motions instead of just being attracted to each other directly and smashing into each other. Why, I don't know...
...unless God made it happen that way!
(Oh no, not again!)