There are a number of offenses which warrant automatic excommunication (i.e., the excommunication is conferred by virtue of the act, without any need for formal pronouncement). These include things like procuring or participating in an abortion, enacting violence against the Pope, illicit episcopal ordinations (this is what got Arb. Lefebvre and the four bishops he consecrated into trouble), simulating priestly faculties when one isn’t a priest (i.e., in the case of invalid ordinations, as of women “priests”), and other things.
Ordinarily, however, one would be excommunicated by formal decree of a bishop. This was done, for instance, by Bp. Bruskewitz in Nebraska, who excommunicated all Catholic members of certain groups, including the SSPX, Call to Action, and the Masons.