First of all, one needs to be aware of the fact that when G-d dictated the Five Books of Moses, He did not do it in the format of a standard "history book". The Hebrew word "Torah" means "instruction". Where it is the aim of G-d to teach a particular lesson through verses of the Torah, in some cases that lesson is best taught in the context of a section where it is out of chronological order. In some places this is obvious, and in some places it can be found by careful study, or simply by referring to the explanations by Rashi.
For the case in point, Genesis 1:14 (And G-d said, "Let there be luminaries in the expanse of the heavens, to separate between the day and between the night, and they shall be for signs and for appointed seasons and for days and years.") Rashi explains:
"Let there be luminaries ([located] in the expanse of the heavens), etc.": They were CREATED on the FIRST day, and on the fourth day, He commanded them to be suspended in the sky, and likewise, all the creations of heaven and earth were created on the first day, and each one was fixed in its proper place on the day that was decreed upon it. That is why it is written [for the FIRST day of creation, in the Hebrew of Genesis 1:1, which can be translated as "In the beginning of G-d's creating WITH the heavens and WITH the earth"]: “WITH the heavens ('ES ha'shamayim') to include [the heavens WITH] their products [the sun, moon and stars, etc.], ”and WITH the earth ('ES ha'aretz')," to include [the earth WITH] its products [the water, dry land, plants, fish, birds, animals, etc.]. — [Midrash Gen. Rabbah 1:14]
With regard to this as well as numerous other "scientific" issues related to the history of the universe and our planet, rest assured that no human being witnessed the creation of the sun, moon and stars. Therefore we do not have any truly scientific information about that nor about the chronology of those events. Modern-day "scientists" have departed from the known boundaries and limitations of the true scientific method, and instead they make pronouncements of their extrapolations and prognostications of prehistoric conditions as "facts", when in true fact their ideas are purely theoretical and based only on unprovable assumptions. And many people accept every new "scientific" theory as a fact, based only on their pure faith in the "scientists". As such, modern cosmology is more like a faith-based man-made religion than a science.
For an analysis of these issues from an authentically scientific basis, please read the content of this web page:
https://asknoah.org/essay/creation-and-science