Aquinas follows Cicero in this (ST 2-2.81.2), arguing that it is a potential part of justice. Justice in the strict sense is equal exchange between equals; but religion can't be the same as justice in this sense, because the relationship between God and creature is inherently unequal. Even pagan gods are at least seen as being superior to human beings, and human beings are held to receive more than they can repay even to pagan gods. Thus the kind of virtue that would govern the relation between God and human worshipper must be one that (1) renders to God His due but (2) does not assume that we can ever do so as full equals. The virtues that do this are all called by Aquinas potential parts of justice. They are justice in the broad sense of the term; they all involve rendering someone their due, but not on the assumption that equal exchange is possible. Religion is a potential part of justice; it is an expression of the fact that our relationships to others are relationships of giving and receiving, sometimes in full exchange of gifts, but in cases like God, or parents, or rulers, merely appropriate token gifts either for extraordinarily great benefits received or for the fulfillment of roles we respect or admire.
It's important to understand that particulars here are secondary. As Aquinas says, the exact details of rendering God His due do not themselves strictly follow from the principles governing the virtue of religion:
It belongs to the dictate of natural reason that man should do something through reverence for God. But that he should do this or that determinate thing does not belong to the dictate of natural reason, but is established by Divine or human law.
The details, in fact, are primarily for our sake; just as gratitude drives a person to try to render some token gift to a benefactor, even if they don't in any strict sense need it, so religion drives us to try to render due to God, even though He doesn't need sacrifices, prayers, or whatever is being offered. Our minds require honoring what we recognize as superior in some way; our intellects find it fitting to give gifts, even if only tokens, to those who have rendered us overwhelming gifts. Thus the actions of religion are very much like the actions that often arise through the virtue of filial piety: children give token gifts to their parents, from whom they receive the gift of life and education. The primary acts of religion are internal -- internal ceremonies or rites, we might say, involving respect or reverence toward God (prayer and devotion, in particular). But because human minds also require close connection to the sensible world, this internal activity expresses itself externally in symbolic form. These are ceremonies and rites as we usually think of them (kinds of corporeal reverence or adoration, as Aquinas calls them, as well as ceremonial gift-givings and gift-receivings).
This account makes religion a moral virtue, and on Aquinas's account all moral virtues observe a golden mean. The most obvious cases of virtues observing a golden mean consists of those that find a mean between extreme passions; but some virtues, and religion is one of them, finds a mean between actions. In other words, religion is not a habit of moderation in the strict sense; it is moderate in the sense that the religious person measures out properly the activities he performs, not rendering them to too many or to too few people; not performing them outside the times they are best performed but not neglecting them, either; and not performing them without regard for actual circumstances. Also like all other moral virtues, religion operates between vices of excess and defect; the primary vice of excess when dealing with our relationship to God is superstition (and, as with religion, this has been the primary sense of the term through most of its history), and the primary vice of defect is irreligion.