Q - What exactly is excommunication?
A - The simple answer is that it is what is called a "medicinal" penalty which a member of the Catholic Church receives in order to bring forth a conversion and repentance because of an action that is contrary to the life of Christ. Iit is called medicinal - because it is given, like medicine, in order to help a person get spiritually healthy.
These are given in order to help the member of the Church who has done something seriously wrong (not all sins merit an excommunication) seek to become reconciled to the Church. They are not "kicked out" of the Church or condemned to hell as some think. They are still members of the Church but are not able to perform certain functions and cannot receive the Sacraments.
Once the person has stopped the action that caused the excommunication (or if it was in the past, resolves not to do it again) and then reconciles themselves with the Church through Confession, they are admitted back into full communion with the Church (note that some excommunications can only be lifted by a Bishop).
Excommunication is a merciful act done to call a wandering sheep back home. The Church's law ins't about power or merely justice, but tends toward mercy.
For a much longer description of excommunication, you can read this.
Related post - How To Respond to Catholics Who Don't Act Catholic.