The shape is determined by minimising the energy of the system for a fixed value of angular momentum.
The energy terms obviously include the (negative) gravitational potential energy, the rotational and internal kinetic energy of the constituents (the material of the planet or the gas and stars of a galaxy).
In the absence of significant rotation, then the energy is minimised in a symmetric spherical scenario. This is the case for planets. Any attempt to collapse along an axis will decrease the potential energy, but at the expense of raising the internal energy of the gas/fluid/material that makes up the planet by even more. Having said that, planets like Jupiter do rotate fast enough to be distinctly non spherical.
You might think that a similar argument would apply to a galaxy. However, what happens is that the increasing internal energy of the gas (NB it is important to note that the flattening occurred before most of the stars formed) in the proto-galaxy could be radiated away as photons. I.e. The gas gets hotter and radiates. This allows the collapse to occur, but a disk results in order to conserve angular momentum.
Now you could say, well why doesn't the same thing happen to a planet? The answer is to do with the relationship between internal energy of the planet material and it's density. Rocky planets are made of relatively incompressible material where there is a huge increase in internal energy (pressure) if they are squeezed. Gas giants are more compressible and more like a galaxy, but their centres are governed by electron degeneracy pressure. Such objects cannot radiate away much of their internal energy and if this dominates the rotational energy, they remain nearly spherical and any collapse is halted.