Ref. No. 1319/42-0000
In the name of Allah the most Gracious the most Merciful
The answer to your question is as follows:
There is no iota of doubt that this question is raised on the basis of ignorance. Today’s science has developed and provided rational and common answers to these questions. In the Holy Qur'an, Allah Almighty stated the purpose of the creation of the universe۔“He is Allah Who has created for you all that is on the earth (Surah Baqara: 29) “And he has subjugated for you whatever there is in the heavens and whatever there is in the earth,all on His own, surely in this there are signs for a people who reflect (Surah Jasiya: 13).
So, all these things in the universe are for the benefit of human beings. Although, there are some things which have perceivable benefits and they are considered to be valuable. While some things are seemingly non-beneficial—rather very harmful—but in factthey are also beneficial to humans. For example, poisonous snakes, scorpions, etc. are very dangerous creatures, but human beings are protected from poisonous germs due to these creatures. If it were not for this animal, man would be destroyed by toxic and deadly germs.
So is the case with the planets. Modern science has also proved that the moon is the closest planet to the earth, which seems to be hanging very close to us like a small round object on our heads. Scientists have researched and estimated that the moon is not less than two and a half million miles away from us. So if we compare the size of the moon with this huge earth, it is 50 times bigger than the earth. Then there is the sun about which scientists say that it is 13 million times bigger than the earth and that it is about 6149 million kilometers away from the earth. Just like the sun, there are many planets above the sun which are multiple times bigger than it and are orbiting in their own orbits at an infinite height by the order of Allah. The stars twinkling on the horizon are in the billions. Astronomers say that there are more stars in the sky than the sand pebbles on all the world's beaches.
Actually man, due to his lack of knowledge, thinks that these planets are created with no purpose, while it is obvious that a wise man does not create any work or anything without a genuine purpose. How can the Almighty Allah create these all things without a purpose? For example: the moon is a planet, about which Allah Almighty Himself has instructed. And by the stars, they find the right way (Surah Nahl:16).
Thus, the Holy Qur'an states that the purpose of the creation of the stars is that man discovers path through them, that they serve as adorn of the sky and they are used as a means of killing the demons. There are many such arguments in the Holy Qur'an.
The Quran says: He it is Who appointed the sun a splendour and the moon a light, and measured for her stages, that you might know the number of the years, and the reckoning. Allah created not (all) that save in truth. He details the revelations for people who have knowledge (Surah Yunus: 5).
Lo! n the difference of day and night and all that Allah hath created in the heavens and the earth are portents, verily, for folk who ward off (evil) (Surah Yunus:6).
هُوَ الَّذِي جَعَلَ الشَّمْسَ ضِيَاءً وَالْقَمَرَ نُورًا وَقَدَّرَهُمَنَازِلَ لِتَعْلَمُوا عَدَدَ السِّنِينَ وَالْحِسَابَ مَا خَلَقَ اللَّهُذَلِكَ إِلَّا بِالْحَقِّ يُفَصِّلُ الْآيَاتِ لِقَوْمٍ يَعْلَمُونَ . إِنَّ فِي اخْتِلَافِ اللَّيْلِ وَالنَّهَارِ وَمَا خَلَقَ اللَّهُ فِيالسَّمَاوَاتِ وَالْأَرْضِ لَآيَاتٍ لِقَوْمٍ يَتَّقُونَ ) يونس/5-6
And Allah knows best
Darul Uloom Waqf Deoband