God Almighty is a never-ending entity possessing all forms of perfection and creating is His grace. Since Allah (swt) is the All-Graceful, granting everything and anything possible, his being graceful calls for him to create all things that can and deserve to be created. So, Allah (swt) creates because He is the All-Graceful. In other words, the reason behind the creation of everything lies in Him being the All-Graceful and since His qualities (in which one of them is His grace) are part of His essence, it can be said that the purpose of His creation is His essence.
Of course, although one who creates something usually does it for his/her own needs and to make up for his/her imperfections and deficiencies, but that isn’t the case with Allah (swt) because He is absolutely self-sufficient and is of no imperfection. Nevertheless, His actions must have a purpose since He is the all-Wise and does nothing without a logical reason and just for nothing. Hence, the system of creation is a system with a specific purpose that is in continuation of Allah’s high goals, therefore no distortion can be found in it.
As for the main purpose of creation, one must say that it is for man to reach perfection and eternal prosperity and the highest virtues and it is man who benefits from reaching such goals because God is the All-sufficient and is in no need of anything, on the contrary, all things are in need of Him, as He Himself says: “O mankind! You are the ones who stand in need of Allah, and Allah He is the All-sufficient, the All-laudable”.
In order to find the answer to this question, a few things need to be cleared up:
1) Allah’s reason for creating the universe:
Paying attention to some points helps us in finding out what His motives are:
Because of Allah (swt) being of necessary existence and independent in it, He is of no limitation, and possesses all perfection.
2- One of the high virtues that His Excellency possesses is His never-ending grace and generosity. In the holy Quran, Allah (swt) says: “…and the bounty of your Lord is not confined.”. Allah (swt) has no limitations on His behalf when it comes to granting and giving, thus if He ever doesn’t grant anything, that means that the person or object that was supposed to receive from Him wasn’t of the required capacity to receive what His Excellency wished to grant. So it is the one to receive who has limitations, not the one who wants to grant.
3- All goodness and virtues arise from existence (wujud), and all imperfections come from nonexistence (‘adam). For instance, knowledge is something that exists, while ignorance is the lack of knowledge. The same goes for strength and weakness.
4- Taken into consideration the third introduction, one can conclude that God’s grace and generosity manifests in Him creating, therefore being the All-Graceful necessitates creation.
In other words, if something ever deserves to be created yet God refuses to create it, it means that God has refused a good thing, because as was said before, all goodnesses and virtues somehow are related to creation. This means that God is being miserly while it is impossible for God to be miserly. Here we conclude that if anyone asks why Allah (swt) created, the answer is that God’s being the All-Graceful is the main cause.
5- God’s qualities and virtues aren’t things added to His essence (there is an argument about God’s qualities and virtues and if they are part of His essence or not; the Shia say that they are part of His essence, yet not causing multiplicity in it unlike some of the Sunnis [Mu’tazilah] who say that they are part of His essence but cause multiplicity, and unlike other Sunnis [‘Asha’irah] who say that they aren’t part of His essence at all but at the same time these virtues are eternal and pre-existent like Allah Himself), while in other objects, they aren’t part of their essence. For instance, the apple has an essence, and it also has some characteristics such as redness and sweetness. Redness and sweetness are all out of the apple’s essence because it can also be green and sour and still be an apple (have the essence of an apple).
The argument of God’s virtues being part of His essence or not is a deep theistic one which can be read about in the chapter on the oneness of God’s virtues and essence in theistic books.
What is important here is that being All-Graceful, is part of His essence (the more accurate way of putting it is to say that this and other qualities and virtues of God are all exactly His essence, not part of His essence, because it isn’t that God’s essence is made up of different parts in which one of them is His being All-Graceful), not outside of His essence. So if it is asked why God created, the answer is that because God is God. So the true reason for Him creating is Himself.
What is important is that although Allah (swt) doesn’t create in order to fulfill His own needs or to make up for His deficiencies because of His self-sufficience and has no deficiencies and is perfect, but His actions surely have a reason behind them because He is the All-wise and the wise one never does vain acts. The system of creation is a system that follows the high goals of Allah (swt) and no signs of disorder can be found in it.
According to Quranic verses, the creator of all things didn’t create them in vain nor for amusement, His creation is on the basis of the truth, and even the smallest particles in this world follow high goals and none of them have been created in vain.
Of course, one mustn’t forget that the final goal for creating the universe was for man to come into existence and in other words, Allah (swt) created the universe in order to create man because he is the highest of all creatures as his creator is the highest of all creators that the universe has never seen. It has been reported that Allah (swt) has said: “Oh son of Adam (pbuh), I have created everything for you, and have created you for myself.”
2) God’s purpose for creation:
What was said till now was all about creation itself, but regarding the creation of man, one must say that the specific virtue that God wanted to achieve by creating man, is the goal of creating man.
The inevitable result of God being All-Graceful, is to create all possible virtues (meaning that His grace calls for Him to create all virtues that can be created). Before the creation of man, He had created other creatures called the angels who possessed all virtues that was possible for them from the beginning of their creation instead of acquiring them like man. Hence, they aren’t capable of achieving any new virtues and they don’t perfect any further. Allah quotes them saying: “For every one of us there is a determined place and position; And verily, we are all ranged in ranks to carry out Allah's commands; And verily, we are those who perpetually celebrate Allah's attributes”.
Now that we have spoken of these two issues, it is time to start the main discussion. No matter what the purpose of creation is, its benefit is for man himself because Allah (swt) is of no need and self-sufficient and it is man who is in true need. Allah (swt) says in the Quran: “O mankind! You are the ones who stand in need of Allah, and Allah He is the All-sufficient, the All-laudable.” Prophet Musa (pbuh) says to Bani-Israel: “Should you be faithless you and everyone on the earth all together [it doesn’t make a difference because] indeed Allah is all-sufficient, all-laudable”.
In the famous sermon known as “the sermon of Hamam”, Imam Ali states regarding this matter: “Allah (swt) created mankind while being in no need of their obedience and servitude towards Him, and (also) being safe from their disobedience because the disobedience of sinners does Him no harm and the obedience of His servants has no benefit for Him (the only reason He has demanded His servants to obey Him and prohibited them from disobedience, is for their own prosperity).
The purpose for creating man has been spoken of in different ways in the Quran. In reality, each of these explanations is pointing to one of the dimensions of the purpose of his creation. Some of those verses are as follows: “I did not create the jinn and the humans except that they may obey Me”, “He, who created death and life that He may test you (to see) which of you is better in their deeds”, “Had your Lord wished, He would have made mankind one community; but they continue to differ, except those on whom your Lord has mercy and that is why He created them (receiving mercy and reaching perfection as a result)”. As you can see, all of these different assertions are in reality following one goal, and that is the development, perfection and guidance of man. As a result, we can conclude that the main and final goal for creating man is for him to reach perfection and eternal prosperity and the highest virtues and human values, and all of these goals can be accomplished through the obedience and servitude of Allah (swt).
Imam Sadiq (as) says that Imam Husein (as) came before his companions saying: “Allah (swt) has created His servants solely for them to know Him, when they know Him, they worship Him, and when they worship Him, they no longer have any need to worship other than Him.”
 یابن آدم خلقت الاشیاء لأجلک و خلقتک لأجلی al-Manhajul-Qawiyy, vol. 5, pg. 516, ‘Ilmul-Yaqin, vol. 1, pg. 381.
 Saffat:164 and 166.
 یا ایها الناس انتم الفقراء الی الله و الله هو الغنی الحمید (Fatir:15).
 و قال موسی ان تکفروا انتم و من فی الارض جمیعاً فإن الله لغنی حمید (Ibrahim:8).
 اما بعد ، فإن الله سبحانه و تعالی خلق الخلق حین خلقهم غنیاً عن طاعتهم ، آمناً من معصیتهم ، لأنه لاتضره معصیة من عصاه ، و لاتنفعه طاعة من أطاعه (Nahjul-Balaghah of Faydh, pg. 11, the sermon of Hammam.
 و ما خلقت الجن و الانس الا لیعبدون (Dhariyat:56).
 الذی خلق الموت و الحیاة لیبلوکم ایکم احسن عملاً (Mulk:2).
 و لایزالون مختلفین الا من رحم ربک ولذالک خلقهم (Hud:118-119).
 ان الله عزوجل ما خلق العباد الا لیعرفوه، فإذا عرفوه عبدوه، فإذا عبدوه استغنوا بعبادته عن عبادة من سواه (‘Ilalul-Shara’i’ of Saduq, according to what al-Mizan has quoted.