Planets and moons are not perfectly round, of course. They only seem spherical, or almost spherical.
Earth, for example, is flattened at the poles, and Jupiter and Saturn, because their extremely dense atmospheres are all we can see, appear even flatter at the poles.
The reason that stars, planets and so forth are even almost spherical, as opposed to a square or some strange shape, has to do with the law of gravitation.
Any bit of matter will attract other units of mass, and as Newton said, the force of this attraction is proportional to the inverse square of the distance between these masses.
It doesn’t matter in which direction these masses are located.
A finite number of uniformly distributed uniform particles would thus tend to coalesce into a spherical clump.
Meanwhile, many other forces are at work in the formation of planets and stars.
We assume that at some time well after the big bang, we have a collection of particles that are not uniform and not uniformly distributed, an inhomogeneous cloud of matter, in which all the particles are attracting each other, but the forces of gravity do not totally balance out.
But there is also along the way some kind of perturbing force that sets the thing rotating.
In particular, you are likely to have a neighboring body, so there is gravitational interaction between the two bodies.
There are also tangled questions of electromagnetism, friction, heat, etc.
So you have gradual coalescence, under the force of gravity, and things beginning to spin, because of inhomogeneities and outside forces.
The result is a roughly but not perfectly spherical rotating body.
The shape is going to be determined by how fast the thing is spinning. The faster it spins, the more oblate it’s going to be, and it depends on the density of matter in the body, too.
Assuming a perfectly spherical billiard ball, for example, it will retain its spherical shape closely, but a rotating water balloon would become quite oblate, bulging around the equator.
In fact, with a heavenly body, you are liable to have so much matter and so high a rate of rotation that matter around the equator will spin off, leaving the body without its “spare tire.”
The spare tire can be dispersed, or under some circumstances can form a roundish satellite, by a similar process.