How could such a Talmid Chacham like Doeg lose his share in Olam ha'Ba?
From Insights to Sotah 21:1:
(b) The CHAFETZ CHAIM in LIKUTEI HALACHOS explains that there are two ways of learning Torah and doing Mitzvos: they can be done either Lishmah or she'Lo Lishmah. Rava -- who says that Torah protects a person from sinning only when a person is involved with it -- is discussing Torah that one learns she'Lo Lishmah, such as the Torah-learning of Doeg (Sanhedrin 106a). However, if a person learns Torah or does Mitzvos *Lishmah*, then the Torah protects him from the Yetzer ha'Ra even while he is not involved in learning Torah, and Mitzvos protect him from the Yetzer ha'Ra when he is involved in them.
The ORACH CHAIM HA'KADOSH (Vayikra 26:3:7) uses this approach to defend the view of Rav Yosef who suggests that Torah protects a person from the Yetzer ha'Ra even when he is not learning. The Gemara rejects Rav Yosef's view, asking how could Doeg and Achitofel have sinned if they learned Torah and the Torah protects a person from sinning? The Orach Chaim answers that Doeg and Achitofel learned Torah she'Lo Lishmah, and that is why their Torah did not protect them while they were not learning. When Rav Yosef explains, according to the Beraisa, that Torah protects a person even when he is not learning, he was discussing Torah that one learns Lishmah.