The Sun formed (experts believe) when a solar nebula contracted. It has been a main sequence star for almost 5 billions years. In another 5 billion years, the Sun will become a giant star. Eventually, its core will shrink to a white dwarf, and then it will cool gradually.