Know that having bad thoughts about someone is forbidden just like having bad speech about him. So just as it is forbidden for you to speak to others about the defects of a person, it is likewise forbidden for you to speak to yourself about that and to hold bad thoughts for him. Allaah says:
“O you who believe, avoid much (types) of suspicion. Verily some (forms) of suspicion is a sin.” [Surah Al-Hujuraat: 12]
And Abu Hurairah (رضي الله عنه) reported that Allaah’s Messenger (صلى الله علیه وسلم) said:
"Beware of suspicion, for indeed suspicion is the most untruthful form of speech." 
The ahaadeeth with this same understanding I have mentioned here are many. What the backbiting of the heart means, is: When the heart has firm conviction and holds bad thoughts about someone. But as for the notions that occasionally display in one's mind or when one talks to himself, then as long as these thoughts do not remain established and continuous in him, it is excused according to the consensus of the scholars. This is because he has no choice in the matter so as to stop it from occurring nor can he find any way to liberate himself from it when it does occur. This is the understanding of what has been authentically established (in the texts).
The Messenger of Allaah (صلى الله علیه وسلم) said:
“Indeed, Allaah has permitted for my ummah, that which their souls whisper to them, so long as they do not speak it out (audibly) or act upon it.” 
The scholars say:
“This refers to the notions that form in one's mind, but do not become settled or established there.”
And they said:
“This is regardless if the notion consists of backbiting, disbelief or so on (i.e. it is pardoned, so long as it does not settle). So (for example) whoever's mind becomes flooded with thoughts of disbelief, but they are only thoughts, without him intending to have them carried out, and he then rids himself of these thoughts right after they occur, he is not a disbeliever nor is there any sin on him.”
We have already stated previously, in the chapter on the (internal) whisperings, the authentic hadeeth, in which the Companions said:
"O Messenger of Allaah! Some of us find things in our thoughts that are too tremendous to speak of." So he (صلى الله علیه وسلم) said: "That is the confirmation of Faith." 
And this goes for the other reports we have mentioned in that chapter that bear the same understanding.
The reason for these thoughts being excused is due to what we have stated previously that these thoughts are impossible to prevent. Rather, one is only able to prevent those thoughts from becoming settled and established in one’s mind. This is why the heart’s being resolute and determined on these thoughts is forbidden.
So whenever these thoughts, which consist of backbiting or any other sin, present themselves in your mind, it is an obligation on you to repel them, turn away from them and mention some excuses or explanations that will change what seems apparent.
Abu Haamid Al-Ghazaalee said in Al-Ihyaa:
“If bad thoughts should occur in your heart, then these are from the whisperings of the Devil, which he has placed in you. So you must deny and reject them because he is indeed the most wicked of evildoers (faasiq). And Allaah has stated:‘If a wicked person (i.e. faasiq) comes to you with news, then verify it, lest you harm people without realizing it (i.e. out of ignorance) and afterwards you become regretful for what you’ve done.’ [Surah Al-Hujuraat: 6]
So it is not permissible for you to believe Iblees (the Devil).
And if there is some sign that indicates that he is corrupt, yet he implies the opposite of that, then it is not permissible to hold bad thoughts (for him).
Among the signs that indicate one holding bad thoughts (for someone) is that your heart changes with him from the way it used to be, and that you run away from him and find him to be unbearable. And that you are lazy when it comes to having concern for him, showing kindness to him and being worried when he does evil. And indeed the Devil comes close to one’s heart when the slightest trace of defects show in people, and he places this in you, while you think (these thoughts) occur due to your astuteness, intelligence and quick alertness. But the believer sees with the light of Allaah. So this person is in reality speaking with the deceptions and the evil plots of the Devil.
And if a trustworthy person informs you of this, then do not believe him nor reject him, in order that you will not have bad thoughts about either of them.
Whenever some evil thoughts about another Muslim come into your mind, then let that make you increase in your showing concern and being kind to him because this will enrage the Devil and repel him from you. So he will not place such thoughts in you afterward out of fear that it will only cause you to increase in your supplication for that person.
And whenever you come to know of a defect or a mistake in another Muslim based on some proofs, which cannot be denied, then advise him in privacy and do not let the Devil deceive you such that he invites you and leads you towards backbiting him. And when you admonish him, then do not admonish him while you are happy and pleased that you have knowledge of his deficiency. So it is as if he is looking at you with the eyes of awe and respect while you are looking down at him with condescending eyes. Rather, make your intention in that to free him from this sin, while you are distressed over him, just as you are distressed when some deficiency enters in you. And his getting rid of that deficiency without you having to admonish him (i.e. on his own) should be more beloved to you than him having to rid himself of it due to your admonishing.”
These are the words of Al-Ghazaalee. I say:
We stated previously that if someone is presented with a notion of bad thoughts for another person, he should cut off those bad thoughts. But this is unless there is some (religiously) legislated benefit that leads to thinking (about that person) in this manner. So if such a reason exists, holding these thoughts about his deficiencies is permissible, as well as warning against them, as can be seen in the jarh (criticism) of certain witnesses, reporters and others we have mentioned in the Chapter on “What Type of Backbiting is Permissible.”
 Saheeh – Reported by Al-Bukhaaree (10/484 of al-Fath) and Muslim (2563)
 Saheeh – Reported by Al-Bukhaaree (5/160 of al-Fath) and Muslim (127 and 202)
 Saheeh – Reported by Muslim (132).
Translator’s Note: This hadeeth shows the Companions’ zeal and enthusiasm in commanding themselves with good and forbidding themselves from evil, such that they would even fight against the evil notions that passed through their minds. But as it is impossible to prevent such thoughts from occurring every now and then, they asked the Prophet about this. His response to them meant that their trying to repel these thoughts showed their strong Eemaan (Faith) and love for good.