It is written (John 3:5): "Unless a man be born again of water and the Holy Ghost, he cannot enter into the kingdom of God." Again it is stated in De Eccl. Dogm. xli, that "we believe the way of salvation to be open to those only who are baptized."
Men are bound to that without which they cannot obtain salvation. Now it is manifest that no one can obtain salvation but through Christ; wherefore the Apostle says (Romans 5:18): "As by the offense of one unto all men unto condemnation; so also by the justice of one, unto all men unto justification of life." But for this end is Baptism conferred on a man, that being regenerated thereby, he may be incorporated in Christ, by becoming His member: wherefore it is written (Galatians 3:27): "As many of you as have been baptized in Christ, have put on Christ." Consequently it is manifest that all are bound to be baptized: and that without Baptism there is no salvation for men. [ST III, Q68, A1]
This is not to say that there is no possibility of salvation for those who are not baptized, as St. Thomas says in the very next article:
Augustine says (Super Levit. lxxxiv) that "some have received the invisible sanctification without visible sacraments, and to their profit; but though it is possible to have the visible sanctification, consisting in a visible sacrament, without the invisible sanctification, it will be to no profit." Since, therefore, the sacrament of Baptism pertains to the visible sanctification, it seems that a man can obtain salvation without the sacrament of Baptism, by means of the invisible sanctification.
Secondly, the sacrament of Baptism may be wanting to anyone in reality but not in desire: for instance, when a man wishes to be baptized, but by some ill-chance he is forestalled by death before receiving Baptism. And such a man can obtain salvation without being actually baptized, on account of his desire for Baptism, which desire is the outcome of "faith that worketh by charity," whereby God, Whose power is not tied to visible sacraments, sanctifies man inwardly. [ST III, Q68, A2 passim]
It is clear, too, that this desire for Baptism may be implicit; it need not be explicit. As we saw previously, the Old Testament Fathers "were justified by faith Christ's Passion, just as we are" (ST III, Q62, A6); but they could not have known explicitly that Baptism would be the means by which they would receive justification.
This is no more nor less than what the CCC teaches us:
The Church does not know of any means other than Baptism that assures entry into eternal beatitude; this is why she takes care not to neglect the mission she has received from the Lord to see that all who can be baptized are "reborn of water and the Spirit." God has bound salvation to the sacrament of Baptism, but he himself is not bound by his sacraments.
Every man who is ignorant of the Gospel of Christ and of his Church, but seeks the truth and does the will of God in accordance with his understanding of it, can be saved. It may be supposed that such persons would have desired Baptism explicitly if they had known its necessity. [§§1257, 1260; emphasis added]