Surah Joseph: Chapter 12 (line 20-30) Yusuf

20. And they sold him for a cheap price-a few coins-they considered him to be of little value.

21. The Egyptian who bought him said to his wife, “Take good care of him; he may be useful to us, or we may adopt him as a son.” We thus established Joseph in the land, to teach him the interpretation of events. God has control over His affairs, but most people do not know.

22. When he reached his maturity, We gave him wisdom and knowledge. We thus reward the righteous.

23. She in whose house he was living tried to seduce him. She shut the doors, and said, “I am yours.” He said, “God forbid! He is my Lord. He has given me a good home. Sinners never succeed.”

24. She desired him, and he desired her, had he not seen the proof of his Lord. It was thus that We diverted evil and indecency away from him. He was one of Our loyal servants.

25. As they raced towards the door, she tore his shirt from behind. At the door, they ran into her husband. She said, “What is the penalty for him who desired to dishonor your wife, except imprisonment or a painful punishment?”

26. He said, “It was she who tried to seduce me.” A witness from her household suggested: “If his shirt is torn from the front: then she has told the truth, and he is the liar.

27. But if his shirt is torn from the back: then she has lied, and he is the truthful.”

28. And when he saw that his shirt was torn from the back, he said, “This is a woman’s scheme. Your scheming is serious indeed.”

29. “Joseph, turn away from this. And you, woman, ask forgiveness for your sin; you are indeed in the wrong.”

30. Some ladies in the city said, “The governor’s wife is trying to seduce her servant. She is deeply in love with him. We see she has gone astray.”

My interpretation:  
This is pretty self explanatory about how Joseph was taken into the home of a wealthy Egyptian by the Mercy of God, as a serving boy and the wife of this man tried to seduce Joseph. Although Joseph was attracted to her, he did not want to sleep with the wife of the man and do a sin against God.  Joseph also had a straight moral compass that didn’t allow him to succumb to his desires.
He ran from the woman, but she ended up tearing his shirt. At the door the husband found them and when the wife tried to lie about Joseph attacking her, another servant suggested that the way it was torn would show the truth. Sure enough, the shirt was discovered to be torn from the back instead of the front, and Joseph was found to be innocent.
The Lord of the home or governor he was working for, told Joseph to forget this event and told his wife to ask forgiveness for her own sins. Many people in town started to talk behind her back and the wife lost her reputation and respect she once had.
This is an example of how consequences of actions do not always wait for the afterlife, your actions in this life, punish yourself in this one at times too.
An artist’s rendition of the attack on Joseph.