There are two, possibly three important pieces to that puzzle. First, we commonly assume that the earth coalesced during the early solar system's broader accretion disk. The bulk collapsed into the main body, our sun. At various points smaller collections were made and those are the planets. Each was an accretion disk area, sweeping its unique piece of space (part of the new definition of a planet, and part of what causes Pluto to be excluded from the planetary list). So the angular momentum of the mass spiraling down upon the center of gravity of the body is what gave it its initial spin.
A second aspect, according to some notions, is that two centers of gravity were somehow formed and the instability is what caused a rift and the moon was spun off. Still in a hot and molten form from the energies of accumulation, and perhaps a fair flash of energy from the sun when it was new, and these hot bodies then spherically formed around their own centers of gravity, a surface crust forming as they cooled.
A possible third aspect that has been given some thought from time to time, admittedly often very little thought but present in discussions nonetheless, is that a planet or such massive body swept by the earth at some very ancient time. Often, that discussion is part of what supposedly brought about the moon, a shattering blow in some billard-ball type action in the early solar system, and that supposedly gave or assisted our planet's spin.
One thing that contributes even today is the moon orbiting our globe. The center of gravity, or barycenter, of the earth and the moon is off-center to the earth's center of mass. This almost mechanically assists in the earth's turn, although there would be some argument over whether this pushes or pulls the cycle along. Next time you look at a bicycle with its two different size major gears, one large and one small, think about this, okay?