In Parshas Mattos we read of the war that God commanded the Jewish people to wage against the Midianites in vengeance against their efforts to lead the Jewish people into sin (as described previously in Numbers 25).
At the conclusion of the war, when the Jewish soldiers returned to the camp with their captives and spoils, Moses was angry with the commanders of the army because they had - in keeping with the normal standards of Jewish warfare - kept alive the women. Moses points out that in this case it was the women in particular who had seduced the Jewish people into sin, and therefore they too had to be killed.
At this point the Torah goes into some detail about various laws of purity, the kashering of the utensils, and the division of the spoils. After all these issues are discussed and dealt with, the Torah tells us that the officers of the army approached Moses with a special request (Numbers 31:48-50):
ויקרבו אל משה הפקדים אשר לאלפי הצבא שרי האלפים ושרי המאות: ויאמרו אל משה עבדיך נשאו את ראש אנשי המלחמה אשר בידנו ולא נפקד ממנו איש: ונקרב את קרבן ה' איש אשר מצא כלי זהב אצעדה וצמיד טבעת עגיל וכומז לכפר על נפשתינו לפני ה':
And the officers that were over the thousands of the army, the captains of thousands, and the captains of hundreds, approached Moses. And they said to Moses, 'Your servants have made a count of the men of war that are under our command, and not one man has been lost. And we have brought an offering to God, what every man has found articles of gold, anklet and bracelet, ring, ear-ring, and kumaz, to atone for our souls before God.'
The Talmud (Shabbos 64a-b) discusses this offering, and provides us with some background as to what was happening here and what it teaches us:
ויקצוף משה על פקודי החיל אמר רב נחמן אמר רבה בר אבוה אמר להן משה לישראל שמא חזרתם לקלקולכם הראשון אמרו לו לא נפקד ממנו איש אמר להן אם כן כפרה למה אמרו לו אם מידי עבירה יצאנו מידי הרהור לא יצאנו מיד ונקרב את קרבן ה'
תנא דבי רבי ישמעאל מפני מה הוצרכו ישראל שבאותו הדור כפרה מפני שזנו עיניהם מן הערוה אמר רב ששת מפני מה מנה הכתוב תכשיטין שבחוץ עם תכשיטין שבפנים לומר לך כל המסתכל באצבע קטנה של אשה כאילו מסתכל במקום התורפה:
"And Moses was angry with the officers of the army." (Numbers 31:14) R. Nachman said, "Rabbah bar Avuha said, 'Moses said to Israel: 'Perhaps you have returned to your first sin (i.e. of fornicating with the Midianite women)?' [The officers] said [in reply], 'Not one man has been lost (i.e. not one man has fallen to sin).' [Moses] said to them, 'If so, why [do you need] an atonement?' They said, 'Though we escaped from sin, we did not escape from [sinful] thoughts.' Thus, 'And we have brought an offering to God.'"
The School of R. Yishmael taught: Why did Israel of that generation need atonement? Because they indulged their eyes with lewdness. R. Sheshes said: Why does Scripture enumerate the outward ornaments (i.e. anklets, bracelets, and rings) with the inner ornaments (i.e. the kumaz, which the Talmud previously explained to refer to an ornament that a woman wore upon her gentalia)? To tell you that one who gazes upon a woman's little finger is as though he gazed upon the genitalia.
The Talmud tells us that the offering of gold by the soldiers was intended to atone for improper thoughts that they had experienced when they looked upon the Midianite women. Although they had not actually sinned with the women, they still recognized that atonement was necessary even for such inappropriate thoughts, given that they resulted from inappropriate gazing. Indeed, the Talmud concludes that not only is it forbidden to look upon a women's actual nakedness, it is even forbidden for a man to gaze upon a forbidden woman who is fully clothed, if he has intent to have pleasure from her beauty. (This is cited by the Rambam (Hil. Issurei Biah 21:2) and Shulchan Aruch (Even Haezer 21:1) as practical halacha.)
However, as important as it is for us to avoid sinful thoughts, there remains a huge difference between sinful thoughts and sinful deeds. The Midrash Lekach Tov (a Biblical commentary written by R' Toviah ben Eliezer in the 11th century) cites the Talmudic passage "מידי עבירה יצאנו מידי הרהור לא יצאנו" - "Though we escaped from sin, we did not escape from [sinful] thoughts" - and then states, "מלמד שהיו כולם צדיקים" - "This teaches us that they were all tzadikim (righteous men)." Despite the fact that they had gazed upon that which was inappropriate - "שזנו עיניהם מן הערוה" - which caused them to have sinful thoughts, the Medrash Lekach Tov still states that they were all tzadikim. It is all too common for those who struggle with sinful thoughts to see themselves as sinful and wicked, perhaps even irredeemably so. From the Medrash Lekach Tov we see that, as serious as this issue is, one who struggles with this problem can still be considered a tzaddik.
I believe there is another important lesson implicit in this Talmudic teaching. Many of the commentaries (Maharsha, HaRif on Ein Yakov) point out that the dialogue between Moses and the officers was actually spread over a fair amount of time. Moses became angry with the officers in verse 14, when they returned with the Midianite women as captives, yet their response does not take place until much later, in verse 48, after they had killed the women, purified themselves, kashered the vessels, and divided the spoils - all of this according to Moses' instructions. All of this took time, at least several days. It was only then that the officers approached Moses with the offering of gold from the soldiers, and informed him that none of the soldiers had actually sinned with the Midianite women.
I believe that this provides us with a model for how one should respond to criticism from one's rebbi (Torah teacher). If your teacher or rabbi accuses you of doing something improper, one's initial response should not be to immediately deny the accusation. Rather, the first thing one should do is to listen to what your teacher says and to follow his instructions. Only later, after one has fully complied with his teacher's instructions, is it time to approach him and to clear your name. (As should be obvious, of course, there may be cases where such an approach is not feasible.)