Gravity is the force that attracts objects with mass toward each other. The greater the mass of an object, the greater its gravitational pull. The closer two objects are to each other, the stronger the gravitational pull between them.
In the solar system, the Sun is the most massive object, so it has the strongest gravitational pull. The planets are less massive than the Sun, so they are held in orbit around the Sun by the Sun's gravitational pull.
The closer a planet is to the Sun, the stronger the Sun's gravitational pull on the planet. This is why Mercury, the closest planet to the Sun, has the shortest orbital period. Neptune, the farthest planet from the Sun, has the longest orbital period.
The planets in the solar system are also held in orbit by their own gravitational pull. This is what prevents the planets from breaking apart as they rotate on their axes.