Explain the role of gravity in the formation of solar systems and galaxies?

Gravity is a force if attraction

A star is made from a nebula which is a spinning cloud of gas and dust and a star is formed when the gas and dust contract under gravity and get a hot enough tempreture and pressure for nuclear fusion and a star is born, that explains the galaxy bit cause a galaxy is a collection of stars.

The planets are formed from left over material from the formation of the sun and the material clumps together because as the material has an attractive force it attracts more and more material which increases the gravity and a planet is formed.