Stars usually start out as clouds of gases that cool down to form hydrogen molecules. Gravity compresses the molecules into a core and then heats them up. Elements do not really form out of nothing in stars; they are converted from hydrogen through a process known as nuclear fusion. This happens when the temperature of hydrogen goes up, thereby generating energy to produce helium. Helium content in the core steadily increases due to continuous nuclear fusion, which also increases a young star's temperature. This process in young stars is called the main sequence. This also contributes to luminosity, so a star's bright shine can be attributed to the continuous formation of helium from hydrogen.
Nuclear fusion is the process through which atomic nuclei of the same charges come together to create heavier nuclei. The process comes with a great release or absorption of energy, which causes the matter bearing the atoms to go into a plasma state--neither gas nor liquid, though it cannot be considered solid either because it is intangible.
After the Main Sequence
At some point, a star will use up the hydrogen found in its core, converting all of it to helium. At this stage, the outer layers of the star will expand to form what is known as a red giant. Hydrogen fusion will now be concentrated on the shell layer around the core and, later on, helium fusion will occur as the star starts to shrink again and becomes hotter. Carbon is created as a result of nuclear fusion among three helium atoms. When a fourth helium atom joins the mix, oxygen is produced.
Only the bigger stars can produce heavier elements. This is because these stars can pull up their temperatures higher than the smaller stars--like our Sun--can. After hydrogen is used up in these stars, they go through a series of nuclear burning depending on the types of elements produced (i.e. neon burning, carbon burning, oxygen burning or silicon burning). In carbon burning, the element goes through nuclear fusion to yield neon, sodium, oxygen and magnesium. When neon burns, it fuses and produces magnesium and oxygen. Oxygen, in turn, yields silicon and the other elements found in between sulfur and magnesium in the periodic table (e.g. aluminum). These elements, in turn, produce the ones that are near iron on the periodic table (e.g. cobalt, manganese and ruthenium). Iron and other lighter elements are then produced through continuous fusion reactions by the above-mentioned elements. Radioactive decay of unstable isotopes also occurs. Once iron is formed, nuclear fusion in the star’s core will come to a stop.