The Earth spins because it formed in the accretion disk of a cloud of hydrogen that collapsed down from mutual gravity and needed to conserve its angular momentum. It continues to spin because of inertia.
The reason it’s all the same direction is because they all formed together in the same Solar Nebula, billions of years ago.