In roughly 5 billion years, the Sun will cool and expand outward many times its current diameter (becoming a red giant ), before casting off its outer layers as a planetary nebula and leaving behind a stellar remnant known as a white dwarf .