Hafetz Haim Halachot
Halacha 46 / 215

Hilchos Lashon Hara, chapter 1, section 7

Sometimes a person is sitting with a group that is saying forbidden things, and there is no way to leave. If the person just sits there silently, the others will think they are crazy. Even so, it is certainly forbidden to join in the forbidden talk.

About this Chazal (our Sages) said (Eduyot 5:6): "Better for a person to be called a madman his whole life, than to be called a wicked person for one hour before the Omnipresent."

At that moment, the person should push themselves with all their strength to hold back. They can be sure they will get a fitting reward from Heaven, because the reward matches the effort. The harder it is to keep a mitzvah, the bigger the reward.

About this, Chazal said: "Every moment that a person shuts his mouth, he merits the hidden light that no angel or creature can conceive of."