The spin of the planets around the Sun, and each around its own axis, was first caused by the original gas cloud having different density in different places. The spin increased because of the contraction under gravity (conservation of energy). So did the flatness of the solar system's shape. As the collapse continues, conservation of angular momentum means that the rotation accelerated. This largely prevents the gas from directly sticking onto the central core. The gas is forced to spread outwards near its equatorial plane, forming a disk, which in turn sticks onto the core.