There are two types of heresy, material and formal. Material heresy is an innocent misunderstanding of Catholic teaching on faith or morals. Formal heresy is sinfully persisting in that misunderstanding even after being corrected by the Church.
Formal heresy, as defined in canon 751 of the Code of Canon Law and paragraph 2089 of the Catechism of the Catholic Church, "is the obstinate denial or obstinate doubt after the reception of baptism of some truth which is to be believed by divine and Catholic faith."
Canon 1364 says heretics are automatically excommunicated. To be excommunicated a person must obstinately reject or positively doubt what they know to be the Church's teaching on faith or morals.
A person commits the sin of formal heresy by deliberately dissenting from some magisterial teaching contained in the Deposit of Faith, that body of public revelation given to the Church by Christ through the Apostles. Canon 750 says:
A person must believe with divine and Catholic faith all those things contained in the word of God, written or handed on, that is, in the one deposit of faith entrusted to the Church, and at the same time proposed as divinely revealed either by the solemn magisterium of the Church or by its ordinary and universal magisterium.
One year before becoming Pope Benedict XVI, Cdl. Joseph Ratzinger, who in 2004 was head of the Congregation for the Doctrine of the Faith, explained how Catholics must believe essential Catholic doctrine but need not accept every exhortation issued on social justice.
"There may be a legitimate diversity of opinion even among Catholics," wrote the cardinal, "about waging war and applying the death penalty, but not, however, with regard to abortion and euthanasia."
A deliberate rejection of official Catholic teaching on faith or morals is a sin that erodes a Catholic's theologically infused virtue of faith. The persistent sin of heresy can lead to a complete loss of supernatural faith in Catholicism. A Catholic's total rejection of Catholicism is apostasy.
Witness the full-scale assault on the Church's teaching in Church Militant's Premium show FBI—Rebellion in the Church.