This story has two layers, and it is important to hear them separately. The first comes from the Qur'an, in Surah Yusuf, the twelfth chapter. The second comes from a famous Persian poem by the Sufi poet Jami, written in 1483.
In the Qur'an, the young Yusuf, son of the prophet Ya'qub, was sold in Egypt and brought up in the house of a high official called al-Aziz. As Yusuf grew into a young man of great beauty and goodness, the official's wife was drawn to him. One day she closed the doors and tried to tempt him into wrongdoing. Yusuf answered, "I seek refuge in God," and refused to betray the trust of the household. He ran to the door, and she caught his shirt from behind and tore it.