I have a rather naive question. In stars such as the Sun, what prevents the whole thing exploding at once? Why is the nuclear fusion happening slowly? I can only assume that something about the fusion is fighting the gravity and slowing the fusion down and when that process is done gravity starts the fusion process again.
The fusion that occurs in the core of the Sun occurs in nothing like the conditions you might be thinking of in a bomb, or a fusion reactor. In particular, it occurs at much lower temperatures and at a much lower rate. A cubic metre of material in the solar core is only releasing around 250 W of power by fusion.
The fusion rate is set by the temperature (and to a lesser extent, density) of the core. This in turn is set by the need for a pressure gradient to balance the weight of material pressing down on it from above. At 15 million kelvin (the core temperature, which is much lower than the temperatures in nuclear bombs or fusion reactors), the average proton has a lifetime of several billion years before being converted (with three others) into a helium nucleus. There are two reasons this is slow. First, you have to get protons, which repel each other electromagnetically, close enough together to feel the strong nuclear force. This is why high temperatures are needed. Second, because the diproton is unstable, one of the protons needs to change into a neutron via a weak force interaction, whilst it is in the unstable diproton state, to form a deuterium nucleus. This is just inherently unlikely and means the overall reaction chain to helium is very slow.
The reason there is no bomb-like explosion is because there is no problem in shifting 250 W per cubic metre away from the core, in the same way that a compost heap, which generates about the same power density, does not spontaneously explode. In the case of a star any additional heat goes into more radiation that diffuses away and in work done in expanding the star. As a result, the temperature of the core is stable. Ultimately, any additional energy emerges as sunlight at the solar photosphere.
If for some reason, the opacity to radiation in the core increased, then the temperature would rise and more energy would be generated by fusion. This is exactly what happens in the core as more hydrogen is turned into helium; the core temperature and luminosity do rise, but slowly, on timescales of billions of years.
If fusion were to proceed faster, the core would get hotter, it would expand and become less dense, and with less density, fusion would slow down.
The main sequence in stars like the Sun does proceed much more slowly than other stages. This is because the p-p chain reaction starts with the fusion of two protons to form a diproton, or helium-2. The diproton is unstable, and usually immediately decays back into two protons, but Bethe realised that on rare occasions it decays by a weak reaction, releasing a neutrino and a positron to form a deuterium nucleus, hydrogen- 2. Because this second process is so rare, it limits the rate of stellar fusion so that stars spend the largest portion of their lives on the main sequence
Fusion in stars requires enormous pressures and temperatures.
Any body, including stars, are subject to their own gravitational field. At any point inside a spherically symmetrical body (which most stars approximate well) the gravitational force will be due to all the mass "below" that point - between that point and the center. That gravitational force obviously points inward.
However all the mass outside that radius is also being pulled inward and exerts pressure on the material below it. This adds to the gravitational force of the material inside.
So enormous pressures exist at the core. As the pressure increases the conditions for fusion become more and more likely. When fusion happens the core region that can allow fusion is kept contained by the pressure from the material outside that core, which cannot fuse. Note that fusion doesn't happen everywhere in the star, just at that core region which has reached high enough pressures.
The energy being generated by fusion keeps everything hot (simplistically) and hot things like to expand and produce an outward pressure. It's the outward pressure from fusing core's thermal energy (which is passed by radiation and convection throughout the star and eventually outside the star as light) that prevents the gravitational collapse of the core due to the force of everything pressing "down" on it.
So it's the gravitational force the body exerts on itself that prevents it "exploding" because it causes fusion which generates heat that pushes against the collapse.
Why is the nuclear fusion happening slowly?
Slow is a relative term, but the rate of fusion is decided by the pressure and temperature inside the star. Oddly enough smaller stars tend to live the longest. This, very simplistically, is because the pressures at the core are relatively low and the amount of fusion that can be maintained by it and the size of the fusing core are correspondingly small. Larger stars have more pressure and larger cores and can burn relatively quickly. The detailed reasons behind the lifetime of stars are somewhat more complex. If you want to read more about this I'd suggest reading e.g. Wikipedia's pages on red dwarf stars and Stellar Nucleosynthesis.
Stars live most of their lives (see main sequence) in a dynamic equilibrium. If the core gets extra hot because of increased heat production, the star expands and the rate of the fusion decreases.
In most cases, the equilibrium is also pretty stable and the star does not oscilate its heat production. Well, some stars DO oscilate their luminosity, but that happen mostly outside of the core (see ex. cepheide).
The equilibrium is sometimes lost (see ex. supernova) and we see a real nuclear explosion "at once". Well, for such a large object nothing happens "at once", the process takes minutes or hours, but it is still pretty fast compared to the lifespan of the star.
p.s. in fact, stars start shining even before they start the nuclear reactor in their cores. The first light comes from the gravitational collapse of the initial gas cloud. The extra heat from the nuclear reactions just stops the collapse for a while (few million or billion years).
An explosion always requires a self-accelerating process. If you set a pile of conventional fuel on fire, it will not explode: it will quickly consume all the oxygen in the surrounding air and the process will slow down waiting for more oxygen to be available. If you want to make an explosive, you will need an oxidizer: a substance which releases oxygen typically in response to temperature. That way, the heat from the fire will release more oxygen which will create more fire etc.
A thermonuclear reaction is accelerated by density. In a hydrogen bomb, the self-acceleration is achieved by starting the reaction around a mass of hydrogen, so that the shock wave from already consumed hydrogen compresses the rest of the load, which itself starts to fuse, creating even more pressure.
In a star, the thermonuclear reaction happens in the core, so the heat it creates pushes the rest of the matter apart, reducing the density and slowing the reaction down. The system then achieves equilibrium in which any change in density is neutralized by the system itself.
You are asking why the fusion is a slow process.
To understand this it is very important to see that the fusion itself means that two protons in the core, separated by the Coulomb forces, must overcome this repulsion. And one of the protons needs to inverse beta decay into a neutron (deuterium nucleus).
It is all QM, and probabilities, and the probability is 1 in 3×10^29 collisions.
Two protons within the Sun fuse. Most of the time the pair breaks apart again, but sometimes one of the protons transforms into a neutron via the weak nuclear force. Along with the transformation into a neutron, a positron and neutrino are formed. This resulting proton-neutron pair that forms sometimes is known as deuterium.
Gravity compression of a lot of "stuff" in something as big as the Sun is stronger than the push from the fusion happening in it. Although it does "explode" when its gravity isn't strong enough to keep the core from pushing the outer layers out by the time fusion makes the element iron (for most stars this happens) leading to the outer layers being lost like your skin and leaving behind a meaty heavy core OR it it might "crunch" if gravity beats pressure (the result can be either a neutron star, lighter original star, or a blackhole, heavier original star.
There is something that "slows the fusion down" actually: radiation pressure. The idea is, nuclear fusion creates nucleons, energy, etc, and also light. Light exerts pressure, it's just very small in everyday life. For very massive stars however, then the radiation pressure becomes important, and it's the reason why gravity doesn't cause the star to collapse.
Radiation pressure sets an upper limit for the mass of a star, because if the star got much bigger it would blow itself apart similar to the explosion you're thinking of. This limit is known as the Eddington limit.
Nonetheless for most stars, radiation pressure is not nearly enough to blow the star apart, and stars don't explode.