The Gemmorah in Kiddushin 81b describes the story of a Rabbi seemingly overriding the [as most agree, Biblical] prohibition of Kiruv Ervah. After a short debate, he finds an excuse that he was acting "for G-d's sake":
רב אחא בר אבא איקלע לבי רב חסדא חתניה.
שקליה לבת ברתיה אותבוה בכנפיה.
אמר ליה לא סבר לה מר דמקדשא?...
מר נמי עבר ליה אדשמואל דאמר שמואל
!אין משתמשים באשה
אמר ליה אנא כאידך דשמואל סבירא לי
:דאמר שמואל הכל לשם שמים
R' Aḥa bar Abba arrived at the house of Rav Ḥisda, his son-in-law.
He took his daughter’s daughter [which was underaged] and placed her on his lap Tos. Ri Hazzaken: he lay with her both naked.
Rav Ḥisda said to him: Doesn’t the Master think that she might already be betrothed? ... Rabbi has likewise transgressed the words of Shmuel: One may not make use of a woman, so how can you hold her on your lap? He replied: I hold in accordance with another statement of Shmuel, as Shmuel says: "All such actions are permitted for the sake of Heaven."
However, on the following page, the Gemmorah brings several examples that even great Rabbis (such as R' Akivah for example) cannot count on their piety when Arayos are around, stressing the severity of those Halochos.
Tosfos develops this idea further Halachicly, allowing accepting women's services: " ועל זו אנו סומכין השתא שאנו משתמשים בנשים:" and Rem"o mentions it also to allow deviations from the Arayos Halochos "for the sake of Heaven" (EH"E 21):
וי"א דכל שאינו עושה דרך חבה רק כוונתו לשם שמים מותר,
לכן נהגו להקל בדברים אלו
A couple of prominent Rabbis in Israel were accused (and sentenced) of sexual harassment and this is the hot topic here. They admitted the transgression but always claimed that they acted "for G-d's sake".
What are the boundaries of permitting different Arayos prohibition (looking at women, using their services, acts of Kirvah, sexual relations) when done for the sake of Heaven?