The Catechism of the Catholic Church, in its discussion of this sacrament, quotes the Letter of James:
Is any among you sick? Let him call for the elders of the Church and let them pray over him, anointing him with oil in the name of the Lord; and the prayer of faith will save the sick man, and the Lord will raise him up; and if he has committed sins, he will be forgiven.
(James 5:14–15, emphasis added; quoted in the Catechism paragraph 1510)
The healing of Christ was not restricted to physical ailments; in fact, one of my favorite Gospel stories is the healing of the paralytic:
People brought to him a paralytic lying on a stretcher. When Jesus saw their faith, he said to the paralytic, "Courage, child, your sins are forgiven." At that, some of the scribes said to themselves, "This man is blaspheming." Jesus knew what they were thinking, and said, "Why do you harbor evil thoughts? Which is easier, to say, 'Your sins are forgiven,' or to say, 'Rise and walk'? But that you may know that the Son of Man has authority on earth to forgive sins"—he then said to the paralytic, "Rise, pick up your stretcher, and go home." He rose and went home. When the crowds saw this they were struck with awe and glorified God who had given such authority to human beings.
(Matthew 9:2–8; New American Bible, Revised Edition)
The Sacrament of Anointing of the Sick is a sacrament of healing in this sense, in which we ask God to send down his Holy Spirit,
who renews trust and faith in God and strengthens against the temptations of the evil one, the temptation to discouragement and anguish in the face of death. This assistance from the Lord by the power of his Spirit is meant to lead the sick person to healing of the soul, but also of the body if such is God’s will.
(Catechism paragraph 1520)
Of course, someone who is specifically impenitent will not be absolved of sin any more than they would be if they went to Confession without penitence. Thus, if the sick person is able, they are encouraged to prepare for and cooperate in the sacrament to the extent that they can:
The sick should prepare themselves to receive it with good dispositions, assisted by their pastor and the whole ecclesial community, which is invited to surround the sick in a special way through their prayers and fraternal attention.
(Catechism paragraph 1516)
But someone who receives the sacrament in good grace will receive "a particular gift of the Holy Spirit" (paragraph 1520). It is for these reasons that the Church believes that the Anointing of the Sick can confer forgiveness of sins.