There are two ways for an excommunication to be incurred:
1) In cases of a canonical court case, or ferendae sententiae; which consists of a Church trial by three judges employing Catholic canon law. These cases are very rare nowadays, but examples of what might bring one about would be that of a lay Catholic or other unauthorized Catholic pretending to be a priest and actually celebrating a Mass; or of a legitimate priest violating the seal of Confession.
2) Various cases of grievous sin, which incur an automatic, or latae sententiae, excommunication. As Kotton pointed out, some examples are heresy or procuring an abortion; others would be apostasy, schism, violation of the Sacred Species (the Body and Blood of Christ in Holy Communion), clerical absolution of an accomplice in a crime, or committing physical violence on the Pope. Another example would be episcopal consecration without authorization from the Holy See, which is what happened to Archbishop Marcel LeFebvre in 1988, when he consecrated several "bishops" without the authorization of the Vatican. By that action, he officially placed himself in schism with the Roman Catholic Church, and all of the "bishops", "priests", and followers of his group, the "Society of St. Pius X", remain in schism to this day.
That's one of the higher-profile cases, but for the most part, excommunications are few and relatively low-key these days; the Vatican's goal is to correct and reconcile those who err, not slap them upside the head and throw them out of the Church. Rome will put up with a lot of nonsense from somebody before they step in and do something, and excommunication is usually the very last resort.
Blessings,
---Wols.