Physicist: This may be the shortest answer yet: “accretion“.
Accretion is the process of matter gravitationally collapsing from a cloud of dust or gas or (usually) both. Before thinking about what a big cloud of gas does when it runs into itself, it’s worth thinking about what happens to just two clumps of dust when they run into each other.
In a perfectly elastic collision objects will bounce out at about the same angle that they came in. Most collisions are inelastic, which means they lose energy and the angle between the objects’ trajectories decreases after a collision. In the most extreme inelastic case the particles will stick together. For tiny particles this is more common than you might think.
Over time collisions release energy (as heat and light). This loss of energy causes the cloud to physically contract, since losing energy means the bits of dust and gas are moving slower (and that means falling into lower and lower orbits). But collisions also make things “average” their trajectories. So while a big puffy cloud may have bits of dust and gas traveling every-which-way, during accretion they eventually settle into the same, average, rotational plane.
Each atom of gas and mote of dust moves along its own orbital loop, pulled by the collaborative gravitational influence of every other atom and mote (there’s no one point where the gravity originates). While the path of each of these is pretty random, there’s always net rotation in some direction. The idea is any cloud in space starts out with at least a little bit of spin. This isn’t a big claim; pour coffee into a cup, and at least some little bits will be turning. That same turbulence shows up naturally at all larger-than-coffee-cup scales in the universe (although typically not much smaller). So, on average, any cloud will be turning in some direction.
Things in the cloud will continue to run into each other until every part of it has done one of three things: 1) escaped, 2) fallen into the center, or 3) moves with the flow. Most of the cloud ends up in the center. For example, our Sun makes up 99.86% of the matter in the solar system. The stuff that stops colliding and goes with the flow forms the ring. Anything not in the plane of the ring must be on an orbit that passes through it, which means that it will continue hitting things and losing energy. Eventually, the “incorrectly orbiting” object will either find itself co-orbiting with everything else in the ring, or will loose enough kinetic energy to fall into the planet or star below. By the way, there’s still a lot of “unaffiliated” junk in our solar system that’s still waiting to “join” a planet.
Those rings are pretty exciting places themselves. Inside of them there are bound to be “lumps” of higher density that draw in surrounding material. Eventually this turns into smaller accretion disks within the larger disk. Our solar system formed as a disk with all of the planets forming within that disk in the “plane of the ecliptic”. One of those lumps became Jupiter, which has its own set of moons that also formed in an accretion disk around Jupiter. In fact, Jupiter’s moons are laid out so much like the rest of the solar system (all orbiting in the same plane) that they helped early astronomers to first understand the entire solar system. It’s hard to see how the planets are moving from a vantage on the surface of one of those moving planets (Earth), so it’s nice to have a simple boxed example like Jupiter.
That all said, those lumps add an element of chaos to the story. Planets and moons don’t simply orbit the Sun, they also interact with each other. Sometimes this leads to awesome stuff like planets impacting each other and big explosions. One of the leading theories behind the formation of our Moon is one such impact. But these interactions can sometime slingshot smaller objects into weird, off-plane orbits. Knowing that planets tend to be found in the same plane make astronomer’s jobs that much easier. From Earth, the ecliptic appears as a thin band that none of the other planets stray from. Pluto was the second dwarf planet found (after Ceres) because it orbits close to the plane of all the other planets, and is inside this band. The dwarf planet Xena and its moon Gabriel orbit way off of the ecliptic, which is a big part of why they weren’t found until 2005 (the sky is a big place after all). Xena and Gabriel’s official names are “Eris” and “Dysnomia” respectively, but I support the original discoverer’s labels, because they’re amazing. So things can have wonky orbits, but they need to do it way the crap out there where they don’t inevitably run into something else. Xena is usually about twice as far out as Pluto, which itself is definitively way the crap out there.
Not all matter forms accretion disks. In order for a disk to form the matter involved has to interact. Gas and dust does a great job of that. But once they’ve formed, stars barely interact at all. For example, when (not if!) the Andromeda and Milky Way galaxies hit each other, it’s really unlikely that any stars will smack into each other (they’re just too small and far apart). However, the giant gas clouds in each should slam into each other and spark a flurry of new star formation. In four billion years the sky will be especially pretty.