About 4.6 billion years ago, a fragment of a giant molecular cloud of gas and dust — the solar nebula — began to collapse under its own gravity, perhaps triggered by the shockwave of a nearby exploding star. Any spinning thing that shrinks must speed up to keep its angular momentum constant, so the collapsing cloud whirled ever faster and flattened into a protoplanetary disk, with the growing proto-Sun hoarding most of the mass at the hot, dense center.
That flattening is the quiet cause of a great deal we take for granted: the planets share a plane and a direction of travel because the disk they condensed from did. The same process is visible today around newborn stars elsewhere in the galaxy — flat disks are simply what collapsing, rotating clouds make.
The gas-rich disk didn't last. Within roughly 5 to 10 million years the young Sun's radiation and winds swept the leftover gas away — a narrow window in which the giant planets had to seize their thick atmospheres or miss the chance. Everything else, including the rocky world we're standing on, would be assembled from the solid debris left behind.