Acting alone, gravity would cause stars to collapse completely, but as long as energy is produced at their centers by nuclear fusion (the joining of atomic nuclei to form new elements, as in a hydrogen bomb), the star is heated and puffed up.
Red giants are created when a star exhausts its supply of hydrogen at its core, with the result that the inner layer contracts and the outer layers expand, forming a redder and much larger star.