If God wants us to worship Him and obey Him, why give us free will?

Allah wants us to worship Him, to show our gratitude to Him for all the blessings He has endowed upon us, and to walk the path of righteousness He intended for us. If He wished He could have made all of humanity submit to His will, worship Him, and obey His commandments without choice – just like angels who greatly outnumber humans are always worshipping Him submissively. Even animals and other creatures are bound by His will to worship and obey Him. God created us as superior beings with intellect and He exclusively granted us free will for us to choose to follow His path out of love and gratitude for Him after understanding that He is the only one who deserves to be worshipped.

It was not God’s plan to program us to worship Him and obey Him like robots without understanding His Lordship. We cannot say that a robot that is programmed to serve a disabled person is a compassionate robot or a computer that is programmed to follow our orders is a loyal computer. Machines just do what they are supposed to do without any feelings or thought – unlike humans who are given the ability to think about and comprehend the realities of life; to feel and understand the emotions of love, hate, happiness, sorrow, and so on. Allah gave us choice so that we may worship Him and obey Him with clear understanding which is more befitting, meaningful, and rewarding.

Free will can be used to produce good or evil, so out of His mercy and wisdom God sent His messengers and revelations to guide us to the path of success, using our intellect and understanding. After all this, if one chooses to go down the wrong path and destroy his life, it is entirely his choice but inherently we were created for good. 

O children of Adam, if there come to you messengers from among you relating to you My verses, then whoever fears Allah and reforms - there will be no fear concerning them, nor will they grieve. But the ones who deny Our verses and are arrogant toward them - those are the companions of the Fire; they will abide therein eternally. Surah A’raaf, chapter 7, verse 35, 36.  

Allah knows best. May Allah guide us.