Alex, our solar system started as cloud of particles that gradually formed into clumps under gravity. When that happens, there is usually some rotational energy (spin) left over unless things freakishly balance out exactly. This comes from all of the particles arriving at different angles and speeds. The same goes for the planets (and even for the galaxy).
Hi alex1998! Yes the Earth spins because of its process of formation. As Drew said, the Earth formed from a massive clump of gas and dust (actually the whole solar system formed this way!). This cloud was spinning initially (maybe a star glanced by it and set it in motion) and as gravity acted to compress it it started to spin faster. This is the same thing as how ice skaters spin faster with their arms in than when they have them out-stretched, the principle is called conservation of angular momentum. So planets, the Sun, and Earth all get its spin from conserving angular momentum as this giant cloud collapsed to form our beautiful solar system. I think in the case of Earth we may also have some spin from the giant moon forming impact too! Amazing to think about but early in Earth’s formation we were actually struck by a giant object Theia (another proto-planet about the size of mars) and a huge mass ejected from Earth to form our moon. This impact probably set the final spin of the Earth, which has been going ever since due to conservation of angular momentum =)