Yes, it's gravity that holds planets and other celestial bodies together. In particular, a solid object on a planet would have to be moving faster than the planet's escape velocity in order to overcome the planet's gravity and go off into space. The Earth's escape velocity, in particular, is about 40,000 km/hour (that's about 25,000 MPH if you're from the U.S.), so essentially no solid objects on Earth move that fast naturally.
However, gasses on a planet do drift off into outer space, which is called atmospheric escape. The most common mechanism for that happening is called Jeans escape. In a gas like the atmosphere, the gas molecules move at a range of different speeds, called the Maxwell-Boltzmann distribution. At any given temperature, most of the gas molecules are moving fairly slowly, but a few of the molecules wind up moving so fast that they're faster than the escape velocity, and the gas molecule goes off into space.
The Sun and other stars have an atmosphere that's much hotter than a planet's atmosphere, which makes it even more likely that a particle will wind up going faster than the star's escape velocity. The particles that escape from the sun like that are called the solar wind.