Chavos Yair (152) discusses a number of these cases.
In some of them, he says, the person delivering the insulting remark is the other one's teacher; the halachah is indeed that if a teacher sees that his students aren't applying themselves properly to their studies, he should "get angry at them and insult them verbally" to spur them to correct behavior (Shulchan Aruch, Yoreh De'ah 246:11).
In others, an apparent insult is in fact a complimentary statement. As an example, in several places where Rav Sheshes says of Rav, "He must have been falling asleep when he said this," Chavos Yair says that it means just that: it would be impossible for a great Torah scholar like Rav to have made a mistaken statement except under such conditions.
And then there are various other types that he lists. (One that I don't understand is his treatment of the expression תרדא\תדורא in a few places: he says that the people who made these statements were "their colleagues and greater than them, so that they didn't mind at all" - yet in one of his examples, in Bava Metzia 20b, the person so addressed does seem to have minded, and there were consequences.)