The Genesis account does not state the timeframe between the creation of the "heavens and the earth" and the events that prepared the earth for habitation.
Genesis 1:1....
."In the beginning God created the heavens and the earth."
This is a singular statement.....a time when God created matter...the physical universe with all that it contains.
Then in verse 2 it speaks about the state of the planet before God began to work on it.....
"2 Now the earth was formless and desolate, and there was darkness upon the surface of the watery deep, and God’s active force was moving about over the surface of the waters."
The creative 'days' were not 24 hour periods, but could have been epochs of perhaps millions of years, wherein God accomplished by increments, all the creative processes he had planned.
The first thing God did was allow light to penetrate the darkness of the earth's surface. With the earth's rotation, light and dark became obvious and were named day and night. We have no way of knowing if humans knew about any of this prior to Moses writing of Genesis. The book of Job has some insights, but it too was written by Moses.
The Bible says that the earth at first was covered in water and then God created an atmosphere where eventually creatures would live and breathe the air( in just the right mixture of gases) that he created there. There was water on the surface of the earth and also above it. No air or water (vital for life) would ever escape into space because God created a way for it to be retained and replenished, perpetually.
Dry land would also make a division in the oceans and various forms of vegetation were the first living things to be created. That is not to say that bacteria wasn't created first, but not mentioned in the creation account. They were never supposed to be a problem for humans, but part of the process of recycling that God put in place to keep the earth from being engulfed by rotting dead material. "Nature" takes care of death by taking everything back into the earth to regenerate it. It was a masterful system....pure genius!
Then God apparently cleared away the remaining cloud layers so that the "luminaries" (sun, moon and stars that he created along with the earth) became visible in the sky for the first time. (Job 38:8-9)
By the 4th "day" things were ready for sentient creatures to appear....and finally man. So with each 'increment' of creation, preparation was made for what came next. By the time sentient creatures arrived on the scene there was already habitats, food, water and air ready for them. All they had to do was reproduce their kind to replace the ones that died naturally. The natural cycle of life and death in the animal kingdom ensured continued life for many other creatures.
The Bible's account makes plenty of room for an old earth and a very long creative process. It just depends on how you interpret the word "yohm" (day) It is clear that it doesn't just mean a 24 hour period because included in the Genesis account (chapter 2:4) which says....
"This is a history of the heavens and the earth in the time they were created, in the day that Jehovah God made earth and heaven." The whole creative process is spoken of as taking place in one "day".
By the time of man's appearance, many creatures (like the dinosaurs) had become extinct, perhaps because they had served a purpose for a time and were no longer needed? God knows but he did not tell us all the details. I am sure he will one day.