As I understand it, excommunication is both a spiritual and a legal penalty. It is both a spiritual warning to the individual AND a legal "scarlet letter" to warn others not to follow the same path. It is both an individual penalty and a warning to the catholic community. The Church's act of banning the individual from certain (but not all) public liturgical functions is a warning/prefigurement to the individual that they will not make heaven unless they repent.
.
The fact that some are released from their excommunication after death shows that the judicial aspect of the church moves slowly. Henry IV obviously repented of his sin before his death and then after he died, the Church held an inquiry into his case, to determine his status. I'm sure they interviewed his confessor and any other priest who had communications with him before he died. These priests would've confirmed that Henry repented. Then, the Church would lift his excommunication, post-death. The reason why it took so long is due to 1) the middle ages' lack of communication/travel/logistics that we're used to. 2) Any legal process (whether Church or secular) does not happen fast.
.
In the case of Pope St Gregory the Great not lifting the excommunication for the abbot, this does not mean the abbot could not be saved. It simply means that Pope St Gregory determined that he did not want an example where an excommunication was lifted easily or quickly. He was making an example of the abbot to show that whatever he did, was gravely wrong. Even if the abbot repented and gained the state of grace, the excommunication would be a spiritual penalty in the sense that it would remind the abbot of his former sin so he would not fall again. (What was he excommunicated for? If it was a sin where it is easy to fall again, i.e. fornication with one of the villagers?, then such a non-lifting of the excommunication would be a wise reminder.)
.
Back in the day, the Church had special monasteries and jails for priests, monks and clerics who violated Church laws. These monasteries were often secluded and penitential. Maybe St Gregory deemed that this abbot needed privacy and seclusion to save his soul? Not all excommunications are for heresy or mental errors. There are many in place for sins of the flesh, drunkenness or even witchcraft. These latter sins are not as easy to "abjure" since they scandalize the faithful with long-lasting consequences. Thus the Church must "set an example" when clerics cross such lines.