The Act of Contrition
Catholic tradition; its roots are in the penitential Psalms (Psalm 51)
O my God, I am heartily sorry for having offended Thee, and I detest all my sins, because I dread the loss of heaven and the pains of hell; but most of all because they offend Thee, my God, who art all good and deserving of all my love. I firmly resolve, with the help of Thy grace, to confess my sins, to do penance, and to amend my life. Amen.
Other common wordings of the last sentence
…I firmly resolve, with the help of Thy grace, never to offend Thee again and to amend my life. Amen.
…I firmly resolve, with the help of Thy grace, to sin no more and to avoid the near occasion of sin. Amen.
In Scripture
Psalm 51:1-4 King James Version
1Have mercy upon me, O God, according to thy lovingkindness: according unto the multitude of thy tender mercies blot out my transgressions.
חָנֵּנִי אֱלֹהִים כְּחַסְדֶּךָ כְּרֹב רַחֲמֶיךָ מְחֵה פְשָׁעָי׃
2Wash me throughly from mine iniquity, and cleanse me from my sin.
הֶרֶב כַּבְּסֵנִי מֵעֲוֺנִי וּמֵחַטָּאתִי טַהֲרֵנִי׃
3For I acknowledge my transgressions: and my sin is ever before me.
כִּי־פְשָׁעַי אֲנִי אֵדָע וְחַטָּאתִי נֶגְדִּי תָמִיד׃
4Against thee, thee only, have I sinned, and done this evil in thy sight: that thou mightest be justified when thou speakest, and be clear when thou judgest.
לְךָ לְבַדְּךָ ׀ חָטָאתִי וְהָרַע בְּעֵינֶיךָ עָשִׂיתִי לְמַעַן תִּצְדַּק בְּדָבְרֶךָ תִּזְכֶּה בְשָׁפְטֶךָ׃
History
There is no single official Act of Contrition. The Church uses many forms of the prayer, and the Rite of Penance gives several. The wording above is the one reference works call the traditional English form; others end differently (see the variants), and the Baltimore Catechism (1885) printed a familiar form of it that stayed standard in the United States through the twentieth century. Where the wording came from before that we have not been able to trace.
The idea behind it is older than any one text. Repentance prayers run through Scripture, above all the penitential Psalms, and Psalm 51 ("Have mercy upon me, O God, according to thy lovingkindness") is the great example, shown here in the King James Bible with its Hebrew.
It is used in the Catholic sacrament of confession and in private prayer.
Medieval tradition. Sources consulted: Baltimore Catechism No. 1 (Project Gutenberg); Wikipedia: Act of Contrition.