According to The Reason Why Mankind Was Created (IslamQA),
Allaah has told us that the creation of the heavens and the earth, and
of life and death, is for the purpose of testing, so as to test man.
Whoever obeys Him, He will reward him, and whoever disobeys Him, He
will punish him. Allaah says (interpretation of the meaning):
“And He it is Who has created the heavens and the earth in six Days
and His Throne was on the water, that He might try you, which of you
is the best in deeds. But if you were to say to them: “You shall
indeed be raised up after death,” those who disbelieve would be sure
to say, “This is nothing but obvious magic”
[Hood 11:7]
“Who has created death and life that He may test you which of you is
best in deed. And He is the AllMighty, the OftForgiving”
[al-Mulk 67:2]
Shaykh ‘Abd al-Rahmaan al-Sa’di (may Allaah have mercy on him) said:
Allaah, may He be exalted, created mankind to worship Him and to know
Him by His names and attributes, and to enjoin that upon them. Whoever
submits to Him and does what is enjoined upon him will be one of those
who are successful, but whoever turns away from that, they are the
losers. He will inevitably gather them together in the Hereafter where
He will reward or punish them for what He commanded and forbade them
to do. Hence Allaah mentions how the mushrikeen (polytheists) denied
the reward or punishment, as He says (interpretation of the meaning):
It's not really about 'what's the purpose of heaven' but 'what's the purpose of the life, death, the creation of mankind, and the reward and punishment (in the Hereafter)' which is explained by IslamQA/