Someone else asked a similar question that was linked here as a duplicate. It seems that question isn't quite the same, but similar enough that it makes sense to put it here.
That person asked:
I know that the Sun is made up of hydrogen and helium.
Also, hydrogen explodes when it touches a flame.
But why does the Sun not explode?
The kind of explosion that happens when hydrogen touches flame is an exothermic chemical reaction in which hydrogen is rapidly combined with oxygen (oxidation). When heat is applied (flame) to hydrogen (H2) in the presence of oxygen (O2), it causes a molecule of O2 to separate and each oxygen atom to bind with a molecule of H2. When this happens, it releases a large amount of energy ( believe this is caused by the breaking of the O2 bonds), and you end up with water (probably steam) and heat (fire).
The kind of "buring" that happens in stars is thermonuclear fission. When two atoms of hydrogen are brought together by immense heat and pressure, they fuse into an atom of helium. In this process, enormous amounts of energy are released. This is the same nuclear reaction that takes place in a thermonuclear fusion bomb. It does not involve a chemical change, but a nuclear one.
Let me take a half-step back on that. A chemical change or reaction is when the atoms rearrange themselves in molecules. In most such reactions, energy is either absorbed (endothermic) or released (exothermic). Sometimes fairly large amounts are released. Explosives such as gunpowder, TNT, C4, and the like are all chemical explosives and mostly their reactions involve very rapid oxidation of the substance being burned. The burning of a match, candle or campfire is the same type of chemical oxidation reaction, but at a much slower pace.
Nuclear reactions come in two main types: fission and fusion. Fission is the splitting of larger atoms into smaller ones. The isotope of uranium with an atomic mass of 235 (92 protons and 143 neutrons) has a half-life of about 700 million years (which is far shorter than the 4.5 billion year half-life of the more common U238). Atoms naturally decay over time, and in a span of 700 million years, a mass of U235 will naturally split on its own and the number of atoms will reduce by half (hence the term half-life). When an atom of U235 splits, it releases a bunch of energy, produces an atom of krypton and one of barium, and releases a few extra neutrons. When one of these neutrons meets another atom of U235, it joins that atom, which destabilizes it, and causes the reaction to happen again. Note: because we are talking about reactions in the nucleus of the atom and resulting changes to the atoms themselves, we are talking about nuclear reactions, not chemical ones.
The reaction I mentioned above takes place in U235 naturally. If you don't have much of it and it's not in a dense enough state, it just sits there like a lump of rock and seems to do nothing. Most of the neutrons it releases don't find another atom to play with, and end up just flying off into space. But if you squeeze it down enough or get enough of it together, more neutrons are meeting up with other atoms than are flying off into nowhere. This creates a chain reaction, which is the basis for nuclear power and nuclear weapons.
A reactor works by controlling the rate of this reaction, keeping it from going too fast. Lumps of uranium are held just close enough together to react,but not rapidly, and rods of neutron-absorbing material (usually graphite) are slid between the lumps to help absorb the neutrons and prevent them from causing more reactions. A nuclear meltdown occurs when the reaction gets out of control and heats up so much that it literally melts the framework holding it all together.
This isn't enough, however, to create a nuclear explosion. That occurs when you apply pressure and squeeze the material down, increasing its density. U235 isn't actually suited for this, but element 94, plutonium, is. When you squeeze plutonium down to a high enough density, this reaction happens extremely rapidly. First one atom of Pu239 splits, releasing its energy in the form of light and heat along with additional neutrons. Then those extra neutrons find other atoms of Pu239 and cause them to split. Every split creates at least twice as many extra neutrons as splitting atoms. The first atom splits to cause 2 to split, which split and cause 2 each for four, then eight, sixteen, 32, 64, 128, 256, 512, 1024, 2048, 4096... each "generation" of these splits is double the previous. This all happens at a time scale measured in tiny fractions of a second, the time it takes light to go only a few millimeters. This all builds up tremendous amounts of heat and pressure, which, unconstrained, erupt out of the core of the bomb.
As it turns out, only a small percentage of the original mass is actually lost to fission. The heat and pressure build up so great so fast that it forces the atoms away from each other in the explosion, thus ending the reaction. In fact, in a fission bomb, before anything other than high-energy x- and gamma rays leaves the bomb case, the reaction is complete, all that's left is for it to expand outward due to the immense pressure.
The other kind of nuclear reaction, fusion, is also used in bombs. In fusion, as I mentioned above, huge amounts of pressure are used to force atoms of lighter elements, typically hydrogen, together. Every time two atoms of hydrogen fuse into one, a huge amount of heat and energy are released (to be honest, I've never understood why energy is released here and not absorbed, but I'm not a nuclear physicist, just a guy who likes to read a lot). In bombs, the 'trigger' for the fusion is actually a fission bomb. The fission bomb creates the heat and pressure necessary, and when hydrogen atoms are added to the mix (usually in the form of tritium or deuterium), the fusion reaction begins and starts adding its heat and pressure to the pot, which helps sustain the reaction long enough to put out an amount of energy several times that which the fusion bomb started.
If you remember back to your high school chemistry lessons, Boyle's Law states $P_1V_1 = P_2V_2$. P is pressure, V is volume. This formula shows that pressure and volume are inextricably interconnected. Charles' law states that $V_1T_2 = V_2T_1$, showing the interrelation between volume and temperature. Gay-Lussac's law, $P_1T_2 = P_2T_1$ shows the relation between pressure and temperature. These all come together into the combined gas law and Avogadro's law. They show that if you increase the pressure, volume, or temperature of a mass, the others are affected as well. An increase in pressure results in an increase of temperature. A decrease of volume creates an increase in pressure which create an increase in temperature. If you increase the volume, the temperature and pressure decrease. Etc...
Ok, so, now here's where it all applies to stars.
In the result of the Big Bang, most of the matter created was hydrogen. There was some helium and a little lithium, but the overwhelming majority was hydrogen.
When the hydrogen molecules floating through space started to clump together through gravity, they started to condense down into smaller and smaller volumes. Separately, the gravity of a single atom of hydrogen is minuscule. But as more and more of them interacted, the combined gravity began to build up. The clouds became more and more compact - denser, and their gravity brought in more and more mass, which added their gravity to the total. Things began to heat up, due to the pressure caused by the volume. The gravity started to get to be enough to continue to pull things toward the center of mass, into the "gravity well" caused by the cloud. As the heat increased due the the pressure caused by gravity, it caused outward pressure. The forces continued to balance each other. As more mass fell in, gravity increased, causing more heat, which caused more pressure, which pushed outward... and so on.
At a certain point, the gravity gets so great, that it exerts enough pressure on the center of the mass to cause nuclear fusion. This, in turn, causes more pressure outward, but the force of gravity of the entire mass is great enough to keep it all together. This is when a star is born. The pressure of gravity has caused a gravitational collapse to ignite into a star. The balance of the forces of gravity and heat and pressure are what keep it going. If there wasn't enough pressure outward, it would collapse further into a singularity (a black hole), if there's too much pressure outward, it would either explode (type II supernova) or at least expand outward until balance, what we call hydrostatic equilibrium, is achieved again.
As a star ages and burns through its supply of hydrogen, the equilibrium changes and at some point it begins to burn helium. After that, carbon. Depending on the original mass, it may fuse other materials up to iron.
In some stars, the mass is enough that when the fusion can no longer be supported, there is no longer enough outward pressure, and the core collapses in on itself. The gravity increases as it shrinks, and there's not enough heat or pressure to overcome it. It collapses to a point of infinite density and becomes a gravitational singularity and a black hole is created. In other stars, the mass isn't quite enough to keep collapsing, and the pressure overcomes it in a massive explosion (type II supernova). In other stars, the star belches out wave after wave of gas as it expands and contracts, finally belching enough away to no longer have enough reactive mass. It shrinks down into a white dwarf - a hot ball of atomic particles that slowly cools over billions and billions of years until they are no longer luminous, becoming a black dwarf (the time for this to occur is believed to be so long that there are, as yet, no black dwarfs in the universe... but there someday should be, long, long in the future).
So, to circle back to the original questions... why doesn't the sun explode, and why doesn't the hydrogen in the sun explode... the main reason is gravity. It reaches a balance. The heat and pressure pushing are balanced against the gravity pulling in. There is no chemical oxidation going on, because the atoms of hydrogen are being fused into helium and there's little oxygen to be consumed in the oxidation process to begin with. Someday we expect the sun will run out of hydrogen to burn. As it starts burning helium, it will expand out past the orbit of Mars, then at some point it will shed its cooler, outer layers and contract into a white dwarf which will slowly cool down to a dying ember in the ever darkening universe.