Our Gemara on Amud Aleph tells us:
Rabbi Abbahu said that Rabbi Elazar said: The fire of Gehenna has no power over Torah scholars. This can be derived by an a Kal V’chomer inference from the salamandra, a creature created out of fire and immune to its effects, and whose blood is fireproof: If a salamander, which is merely a product of fire, and nevertheless when one anoints his body with its blood, fire has no power over him, all the more so should fire not have any power over Torah scholars, whose entire bodies are fire, as it is written: “Surely My words are as fire, says the Lord” (Jeremiah 23:29), and the words of Torah become part of the Torah scholars’ very bodies.
A couple of interesting points regarding this Aggadah. Maharsha asks if this is so, why did we see earlier (15b) that smoke was rising from Elisha ben Avuya’s grave, which certainly implies that he was suffering from the fires of Hell. The Maharsha answers that one who understands God and still rebels receives an even more potent punishment, and even a Torah sage cannot be protected from that particular kind of Hellfire.
Ben Yehoyada asks how does this Kal V’chomer make sense? Obviously Torah sages are not fireproof. We have not known typical Torah sages to emerge unscathed from flames, yet the Gemara’s creature called the Salamandra was fireproof? Ben Yehoyada answers the fire of Gehenom and the fire of the Torah is a spiritual form of fire. Thus the Kal V’chomer goes as follows: if covering yourself with the blood of a salamandra can protect you from earthly fire, surely the sages whose entire essence is imbued with the spiritual fire of Torah, will be protected from spiritual fire of Gehenom.
Of course ultimately none of this makes any sense on a superficial level. If Gehenom is a punishment, either you deserve it or you don’t. And if a person repents that might help, but it has nothing to do with Torah per se.
When trying to decode Aggadah, it is helpful to think in global impressionistic terms, because sometimes the overall message may emerge even if all the details are not clear. Much like when you look at an impressionist painting you need to step back to see the picture. (Rambam in his intro to the Moreh Nevukhim explains that with some aggados, the details have no meaning at all, and it is just the overall message that is important.)
So the overall theme is that Torah seems to be offering some protective factor and it is described as a fire that combats a fire. What seems to be relevant here is that two similar forces counteract each other. It is fair to say that it means the passions of Torah counteract and distract against the more evil and physical fires of lust. As the Gemara Kiddushin 30b says,
The Holy One, Blessed be He, said to Israel: My children, I created an evil inclination, which is the illness, and I created Torah as its antidote. If you are engaged in Torah study you will not be given over into the hand of the evil inclination, as it is stated: “If you do well, shall it not be lifted up?” (Genesis 4:7). One who engages in Torah study lifts himself above the evil inclination.
However, Maharsha says there are limits to this protection, such as one who should know better and still rebels. I take this to mean that sincere engagement in Torah will counteract susceptibility to impure desires that might, so to speak, come out of the blue. However, Torah will not protect from premeditated acts, in the same way that, true to the metaphor, Elisha ben Avuya’s Torah did not protect him from the fires of Hell, despite his deep scholarship.