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.
“O you who believe, avoid much (types) of suspicion. Verily some (forms) of suspicion is a sin.” [Surah Al-Hujuraat: 12]
And Abu Hurairah (radhi Allaahu anhu) reported that Allaah’s Messenger (sall Allaahu alayhi wa sallam) 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 since he has no choice in the matter 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 (sall Allaahu alayhi wa sallam) 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 (sall Allaahu alayhi wa sallam) 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 excuse or explanation that will change what seems apparent.
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, narrators 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 (sall Allaahu alayhi wa sallam) about this. His response to them meant that their trying to repel these thoughts showed their strong Eemaan (Faith) and love for good.
Link to the this post : http://salaf-us-saalih.com/?p=34083
Source : Guarding the Tongue – Imaam an-Nawawee (Al-Ibaanah e-Book)