Allah tells;
وَلَقَدْ هَمَّتْ بِهِ وَهَمَّ بِهَا لَوْلا أَن رَّأَى بُرْهَانَ رَبِّهِ
And indeed she did desire him, and he would have inclined to her desire, had he not seen the evidence of his Lord.
This is about the thoughts that cross the mind, according to Al-Baghawi who mentioned this opinion from some of the analysts.
Al-Baghawi next mentioned here a Hadith that he narrated from Abdur Razzaq, from Ma`mar, from Hammam, from Abu Hurayrah, from the Messenger of Allah,
يَقُولُ اللهُ تَعَالَى
إِذَا هَمَّ عَبْدِي بِحَسَنَةٍ فَاكْتُبُوهَا لَهُ حَسَنَةً فَإِنْ عَمِلَهَا فَاكْتُبُوهَا لَهُ بِعَشْرِ
أَمْثَالِهَا وَإِنْ هَمَّ بِسَيِّيَةٍ فَلَمْ يَعْمَلْهَا فَاكْتُبُوهَا حَسَنَةً فَإِنَّمَا تَرَكَهَا مِنْ جَرّايِي فَإِنْ عَمِلَهَا فَاكْتُبُوهَا بِمِثْلِهَا
Allah the Exalted said,
`If my slave intends to perform a good deed, then record it for him as one good deed; if he performs it, then record it for him multiplied ten folds.
If he intends to commit an evil act but did not commit it, then record it for him as one good deed, if he left it for My sake. But if he commits it, then write it as one evil deed.'
This Hadith was also collected in the Two Sahihs using various wording, this is one of them.
It was also reported that the Ayah means that;
Yusuf was about to beat her.
As for the evidence that Yusuf saw at that moment, there are conflicting opinions to what it was.
Ibn Jarir At-Tabari said,
"The correct opinion is that we should say that he saw an Ayah from among Allah's Ayat that repelled the thought that crossed his mind. This evidence might have been the image of Yaqub, or the image of an angel, or a divine statement that forbade him from doing that evil sin, etc.There are no clear proofs to support any of these statements in specific, so it should be left vague, as Allah left it.
Allah's statement next,
كَذَلِكَ لِنَصْرِفَ عَنْهُ السُّوءَ وَالْفَحْشَاء
Thus it was, that We might turn away from him evil and immoral sins.
means, `Just as We showed him the evidence that turned him away from that sin, We save him from all types of evil and illegal sexual activity in all his affairs,'
because,
إِنَّهُ مِنْ عِبَادِنَا الْمُخْلَصِينَ
Surely, he was one of Our Mukhlasin servants.
meaning, chosen, purified, designated, appointed and righteous. May Allah's peace and blessings be on him.