He gives guidance to whomever He wills, protects them and keeps them safe from harm as an act of grace. And He leads astray whomever He wills, abases them and afflicts them out of His justice.
The principle of testing faith comprises of a number of dimensions, of them is:
1) Allāh affording His creatures the ability and means to overcome these trials and tribulations. He tests them, but is also Just. He did not leave them helpless, without power to pass the tests that are to come their way. This means that human beings and the jinnkind have the ability to choose how they live their lives and deal with these tests. This indicates that they have a God-given free will.
2) Allāh is the All-Wise and the Most Just, He will reward people for remaining true to their faith in difficult times and punish those that turn away in rebellion.
3) As Allāh is the Creator, the Master, and in control of everything; what He has given to his creatures of a free will, only operates within the parameters of His Own Irresistible Will. This clarifies that the free will of people is bound to the Will of Allāh. This is how we understand His Words: ‘And you do not will except that Allāh wills. Indeed, Allāh is ever Knowing and Wise’.
Guidance is what one needs to succeed in life and it comes only from Allāh, Exalted is He. If Allāh so willed to guide someone, none could misguide him; and if Allāh willed that someone would be misguided, none could guide him. The Prophet (SAW) used to ask in his supplications: ‘O mover of hearts, make firm our hearts towards obeying You. O turner of our hearts! Turn our hearts towards Your religion.’ Anas asked the Prophet (SAW): ‘O Messenger of Allāh! We believe in you and in what you have brought. Then do you still fear for us?’. The Prophet (SAW) was teaching us that guidance comes only from Allāh and that seeking the guidance of Allāh is part and parcel of a believer’s life, he implores Allāh for it in every prayer when he says: ‘Guide us to the Straight Path’.
The author said: ‘He gives guidance to whomever He wills…as an act of grace’, which clarifies that had Allāh Willed He could have dictated who would be guided and who would be misguided, however, Allāh guides people out of His grace (faḍl). Many people are confused about how it is that Allāh misguides some people out of justice. The answer is simple, yet, it should be made clear from the outset that the matter of being guided is far from arbitrary or a case of favouritism, Exalted and Elevated is Allāh from such.
Allāh placed inside of every human being an internal moral compass (fiṭrah) that gives a call from within to adhere to good and abhor evil. He then sent Messengers to call the people to His religion and adhere to His prescriptions; thus guiding them further. And He placed signs in all creation that point to His existence and Lordship.
The Qur’ān asserts in clear terms that Allāh does as He Wills; for He is: ‘the Doer of whatever He Wills’ and this indicates in a general sense the words of the author: ‘He gives guidance to whomever He wills’. Furthermore, Allāh said: ‘Had We so willed We could have given guidance to everyone’, clarifying that Allāh’s guidance is more than simply showing human beings right from wrong and granting them access to revelation, as this has already been afforded to all of mankind after the advent of the last Messenger (SAW) with the Qur’ān. The Qur’ān states: ‘When Allāh desires to guide someone, He expands his breast to Islam’, which reveals another dimension to the matter of guidance. Likewise misguidance is more than simply allowing human beings to commit sin and disobey His commands. The Qur’ān spoke about the believer who entered paradise and said: ‘Had it not been for the grace of My Lord, I should have certainly been among those brought there’ referring to the inhabitants of the Hell Fire. Both of these verses clarify that: ‘Allāh sends astray whom He wills and He guides on the Straight Path whom He wills.’
The reality of Allāh’s guiding and misguiding man is found in His words in the Quranic chapter al-Layl: ‘As for him who gives [to others] and is conscious of God’; meaning the person who used his god-given ability to adhere to Allāh’s commands,: ‘and confirms the Good’; meaning whilst he was believing in Allāh’s Oneness,: ‘We will pave his way to Ease’; such a person will be guided by Allāh to do more good deeds that will ultimately enter him into paradise. These verses illustrate that Allāh is indeed guiding people and that His guidance is based on the will of the person. If the person turns to Allāh in devotion, Allāh in turn responds with guidance and opens the way for goodness for that person. The more devoted and sincere a person becomes, the more Allāh guides him and eases the path ahead for him to do more good deeds and earn his place in paradise. It is because he sought out guidance that Allāh guided him; and then: ‘for those who accept guidance, He increases their guidance, and bestows on them their piety.’ which is additional guidance to act more righteously. This is all from the grace (faḍl) of Allāh and His Justice (‘adl).
Conversely, when a person turns away from Allāh, Allāh turns away from him and leaves him to stray. The verses of the Quranic chapter al-Layl continue: ‘But he who is a greedy miser and thinks himself self-sufficient’; in contrast to the first person, this person was turning away from Allāh’s Commands and Guidance, ‘and denies the Good’; disbelieving in Allāh’s Oneness, ‘We will pave his way to Difficulty’; based on his turning away from Allāh, neglecting His commands, not desiring His guidance, Allāh will in turn misguide him by making it easier for him to commit sins, misdeeds, and shameful actions.
The Prophet (SAW) taught this belief to his Companions. Ali (RA) said that they were once sitting with the Prophet (SAW) when he said: ‘There is not a single one of you except that his seat has been written (already) in paradise or in the hellfire’. They responded: ‘O Messenger of Allāh, shall we not then surrender ourselves to that?’, the Prophet (SAW) answered them: ‘No! Work (i.e. do good deeds), for everyone will be eased towards what he was created for’, and then he recited the aforementioned verses of al-Layl. His response to their question was to a) affirm that they must strive to do good deeds, and b) what they do will impact directly on how Allāh will guide them and pave a way for them ultimately into paradise.
The above mentioned elucidation on how Allāh guides and misguides His creatures is based on a holistic view of the Divine Decree (qadar) of Allāh as explained in the Qur’ān and the Sunnah. The way in which the Sacred Texts speak of Allāh’s Decree is truly profound and yet simple and logical. However, if one focuses on certain aspects of the textual evidences, or perceives them from only one dimension they will misunderstand the whole concept of Divine Decree (qadar). For instance, if some people were to simply focus on those textual evidences that confirm that Allāh guides and misguides people, they would conclude that human beings in reality have no free will. This is in fact the position taken by the ancient sect known as the Jabriyyah. This sect believes that the human being has no free will and has no ability to choose how he acts. For them, whatever a human being does was dictated upon him by Allāh and he has no control over what he does.
Secondly, when Allāh said to His Messenger (SAW): ‘You cannot guide those you would like to but Allāh guides those He wills’, referring to his dear uncle Abu Tālib, indicates guidance in another sense, and that is the internalizing, inwardly accepting, and submitting to the general guidance of Allāh. Allāh asserts in clear terms that He is the only One who guides in this sense of the word by expanding a person’s: ‘breast to Islam’. 
As for those who are misguided, they were misguided because they rejected the guidance of Allāh in the first place, going against the internal moral compass (fiṭrah) Allāh endowed in them; thus Allāh debases them further due to their initial sin of rejecting His general guidance and suppressing the call from within. Allāh, Exalted is He, is not unjust in the least to anyone. Only when people turn away from Him, does He then misguide them. Such as the case of the Children of Israel: ‘… when they deviated, Allāh made their hearts deviate. Allāh does not guide people who are deviators.’
In this way did Ahl Sunnah adhere to the middle-way, asserting that only Allāh can guide without any injustice on His part, and yet man has choice in life. True are the words of the All-Mighty: ‘Whoever Allāh guides is truly guided. But if He misguides someone, you will find no protector for them to guide them rightly.’
1) Without exception, Allāh’s misguidance of people is in response to their rebellion. In each of the thirty-one Quranic verses which mentions that Allāh misguides people, the Qur’ān either explicitly states that He misguides transgressors, the unjust, those who belie His signs, and so on, or the preceding verses clearly refer to such people. For instance, Allāh says in the 7th verse of al-Baqarah: ‘Allāh has sealed their hearts and their hearing, and over their eyes is a veil; and horrendous suffering awaits them’ after He mentioned: ‘those who are bent on denying the truth’ in the 6th verse. One of the clearest verses that explains why Allāh misguides is that of the Quranic chapter al-Naḥl wherein Allāh says: ‘As for those who do not have iman in Allāh’s Signs, Allāh will not guide them and they will have a painful punishment.’ [16:104]
2) When we turn away from Allāh, we make ourselves vulnerable to suffering, pain, and misfortune. One should not consider misguidance in a limited way, confined only to a corruption of faith and conduct. Misguidance is of many types and from them is misfortune in one’s well-being and quality of life. Allāh spoke of a town that were beset with financial misfortune, explaining: ‘If only the people of the cities had had faith and god-consciousness, We would have opened up to them blessings from heaven and earth. But they denied the truth so We seized them for what they earned.’ [7:96]. We ask Allāh to save us from misguidance and keep us firm upon His religion.