Allaah (The Most High) said:
وَمَا خَلَقْتُ الْجِنَّ وَالْإِنسَ إِلَّا لِيَعْبُدُونِ
”And I did not create Jinn and Mankind except that they should worship Me (alone)” [Soorah Dhariyaat: Ayah: 56]
The worlds of the creatures with intellectual discernment are three (in number):
 The world of (creatures with intellectual discernment) in which there is only good and no evil, and they are the angels.
 The world of (creatures with intellectual discernment) in which there is only evil and no goodness, and they are the devils.
 The world of (creatures with intellectual discernment) in which Allaah (gives them the freewill to choose between) good and evil, and there is more goodness amongst them. The other world (in this third category) is that of (those creatures with intellectual discernment) in which Allaah (gives them the freewill to choose between) good and evil and there is more evil amongst them.
The world of Jinn and Mankind are the worlds in which Allaah (allows them and gives them the freewill to choose between) good and evil, and Allaah created them to worship Him (alone). The devils are a species from the Jinn, but they rebelled and all of them became evil. And that is why Allaah (The Most High) said: [وَمَا خَلَقْتُ الْجِنَّ وَالْإِنسَ إِلَّا لِيَعْبُدُونِ] ”And I (Allaah) did not create the Jinn and Mankind except to worship Me.”
The devils are a species from the Jinn, and Allaah created Jinn and mankind to worship Him (alone), as He stated in this (above) ayah. And amongst them (i.e. Jinn and Mankind) are those who fulfil the worship of Allaah (alone) and they are the believers, and amongst them are those who do not fulfil it; rather they wilfully turned away and became arrogant, and they are the various types of disbelievers.
It is sufficient for us to know that Allaah created us to worship Him (alone) and it is obligatory upon us to fulfil the purpose for which Allaah created us. And worship is obedience alongside humility and lowering oneself in the presence of Allaah [The One, The Irresistible]. And when it is the case that the nations did become overwhelmed with ignorance, apathy and forgetfulness, and busied themselves with the present worldly life and became heedless of the afterlife, Allaah sent Messengers to every nation to clarify the purpose for which they were created and brought into existence.
Allaah (The Most High) said:
[وَلَقَدْ بَعَثْنَا فِي كُلِّ أُمَّةٍ رَّسُولًا أَنِ اعْبُدُوا اللَّهَ وَاجْتَنِبُوا الطَّاغُوتَ]
”And verily, We have sent among every Ummah (community, nation) a Messenger (proclaiming): “Worship Allah (Alone), and avoid (or keep away from) Taghut (all false deities, etc. i.e. do not worship Taghut besides Allah).” [Soorah An-Nahl: Ayah: 36]
So Allaah informed (us) that He sent Messengers to the slaves–commanding them to worship Allaah (alone) and to stay away from all false deities.
For Further details see: Ash-Sharh Al-Moojaz: page 10 onwards