It is thought that solar systems form when part of a diffuse cloud of gas (mostly hydrogen and helium) begins to collapse due to its own gravity. This is called a pre-solar nebula. At the center, a star forms, and much of the remaining material ends up rotating around the star in a flattened disc. This so-called protoplanetary disc eventually agglomerates into orbiting planets, which build up from the material in the disc over time.
Protoplanetary discs have been observed by astronomers around young stars close by. The sun, the Earth, and our fellow planets likely formed the same way. Nearly everything in our solar system turns in a counter clockwise motion, which means our protoplanetary disc and the nebula that lead to it must have been spinning this direction.
Why rotation at all? Anytime a lot of mass ends up in one place (the protostar), gravity there gets very strong. This pulls in the surrounding material (the disc). Anytime something spinning a little bit gets pulled in, it speeds up. (Watch a spinning ice skater pull their arms in—or try it in your desk chair!) So, the disc ends up rotating quite a bit.