As Salaamu Alaikum
Below is my response to a seeker of the truth, who asked me the following question:
"God does not need our worship, so why did He create us to worship Him? Why does He allow all the evil that happens on earth and why punish people when He already knows what they will do. Why create people if He is going to doom them to eternal Hell anyway?"
I hope my answer is satisfactory and I ask Allah to guide us all to what is best, please feel free to add on any more points that you feel can help:
Firstly, I would like to mention that that is a very good question and one that I myself thought about a lot. I also gauge from the question that you believe in God yet can not understand why He would do this to His creation, so I will try, with the Help of God, to answer as best as I can.
The first point we should realize is that God is beyond us in many ways, He has more knowledge and wisdom than we ever can, so this should humble as and make us realize there are many things God does which is beyond our understanding yet we need to trust His infinite wisdom which will reveal itself over time. So anytime, we do not understand something about God, it is because of our own lack of knowledge. The key is to ask and learn, which you did, May God bless you with guidance and understanding, yet know that not everything about God can be understood by us.
Regarding why did God create us, the simple answer of to worship Him still leaves many questions, some of which you have mentioned, and you are right, God does not need our worship, but we do. We are in need of God, and the only way to get His help and Mercy is to worship Him in the way that He has revealed through His prophets. We need God's worship so that we can gain peace of mind, knowing our purpose and living for it, so that we can gain nearness to Him, so that we can gain Paradise and escape the Hellfire.
That still leaves the question, why create humans and put them through this test and punish them if they fail? My question to you would be why not? God is the Creator, and Creating is what He does, just like painters paint and builders build, God creates and He decides what to create. If you had to paint a picture of an ugly man, the picture can not complain to you as to why you made him ugly, that was your chose and he is your painting, similarly we are God's creation and God tells us in the Quran, "He is not answerable to you, but you all are answerable to Him,"
The Creator did what He does best, He created. He created an amazing universe with amazing creatures, most amazing of them all He created the human being and gave it intellect and the ability to choose and to do both good and evil. But the Creator is also most Wise and knows that if He creates a being like a human, there has to be rules and punishments, if not humans will do what they want and chaos will reign. Just like we set rules in our countries, schools, and workplaces, and we set punishments for those who break the rules, yet nobody says it is unjust, rather we say it is unjust not to have punishments, the same applies to God on a higher level for God has more right to make rules for us and to punish us than other people since He created and owns us.
Imagine if God did not create the Hellfire and if He decided all humans would go to Paradise, what would be the use of me doing good, since the murderers, rapists, terrorists, etc would get the same reward as the doers of good? It would be unjust and illogical, and God is most just and most logical.
Imagine if He just placed us in paradise and Hell without testing us on earth first since He already knows our endings, the people in Hell would feel it is unjust to them, yet by letting us live our lives and giving us this chance, if anybody ends up in Hell, there will be no excuse he can make, that is the Wisdom and Justice of God.
In the end, we must realize that we do exist, God has already created us and placed us in this test, will we choose to submit (Islam) to Him and worship Him because we need to, or will we choose to rebel and face a punishment for being ungrateful to our Creator who gave us so much in this world and is offering us so much more in Paradise the Hereafter if we choose to worship Him.
Think about it but more importantly pray to God and ask Him to grant you the guidance and understanding you need and are searching for.
May God guide us all
Peace upon those who seek guidance