The planets, with the exception of dwarf planet Pluto, lie in the same relatively flat plane because the solar system formed from a collapsing disk of spinning material 4.5 billion years ago. A cloud of material condensed into an accretion disk of matter that spun around the sun, and particles that did not coalesce into planetary bodies were blown away by strong solar winds.
The plane in which the planets orbit is called the ecliptic. Pluto has an inclination of 17 degrees from the ecliptic. Mercury, the innermost planet, has the greatest eccentricity from the ecliptic, at 7 degrees. A NASA photograph illustrates the moon, sun, Mercury, Mars and Saturn all in the same relative plane of the solar system. Mercury, Mars and Saturn appear to be close to a straight line formation.
Planets came together because of the force of gravity. Hydrogen and helium created the core of the sun. Leftover materials turned into planets, with heavier elements closer to the sun and lighter elements farther away. A disk shape formed because particles collided, and these particles either formed larger masses in the same plane, or they were ejected to outer parts of the solar system. Scientists are not sure how particles coalesced in certain ways in the primordial solar system, but when more particles got together, the effect snowballed into bigger planets in the same spinning plane.