The short answer is, the law of angular momentum.
It's not just the earth that spins. All planets tend to spin.
In fact, if you think about it, there is a rotation of galaxies that form out of huge disks of matter. A solar system forms in a similar fashion, and exhibits a rotation around the center of mass (the sun, which is also rotating). Planets form in a similar fashion, and exhibit a rotation around their own center of mass (their axis) with moons rotating around them as well.
What causes this? Current theory holds that solar systems naturally form where an eddy causes a thickening of galactic material. So the spin is "built in" to the fact that the density is slightly higher. As the material contracts, the rotation rate increases like an ice skater pulling their arms in tight.
The same holds for a planet. An eddy in a "solar disk" can cause a contraction and accretion of material. The eddy means it will have a built-in rotation around its own center of gravity.
There are many things that can upset that spin. Collisions with other bodies, near-misses by larger bodies. Tidal locking. Liquid vs solid core. But that's the gist.
If you would like more information, here is a good explanation:
Why and how do planets rotate?