Then when he says because by the works of the law, he assigns the reason for these words: first, he assigns the reason; second, he explains it, at for by the law.
First, therefore, he says: the reason there is not any man just is that no flesh shall be justified before him, i.e., according to his judgment, by the works of the law; because, as is stated in Galatians: if justification were through the law, then Christ died to no purpose (Gal 2:21), and in Titus: not because of deeds done by us in justice, but in virtue of his own mercy he saved us (Titus 3:5).
However, a work of the law is of ...two kinds: one is peculiar to the Mosaic law, as the observance of ceremonial precepts; the other is a work of the law of nature, because it pertains to the natural law, as you shall not kill, you shall not steal, etc. Now some take the Apostle's words as referring to the first works, namely, that the ceremonials did not confer the grace through which men are made just. But this does not seem to be the Apostle's intent, for he immediately adds: for by the law is the knowledge of sin. But it is clear that sins are made known through prohibitions contained in the moral precepts. Consequently, the Apostle intends to say that by no works of the law, even those commanded by the moral precepts, is man justified in the sense that justice would be caused in him by works, because, as he states below: and if by grace, it is not now by works (Rom 11:6).
Then when he says, for by the law, he proves his statement, namely, that the works of the law do not justify. For the law is given that man might know what to do and what to avoid: he has not dealt thus with any other nation; they do not know his ordinances (Ps 147:20); the commandment is a lamp and the teaching a light and the way of life (Prov 6:23). But from the fact that man knows a sin he should avoid as being forbidden, it does not at once follow that he avoids it (because this pertains to the nature of justice), because concupiscence subverts the judgment of reason, when it bears on a particular moral action as performable. Consequently, the law is not enough to make one just; another remedy is needed to suppress concupiscence.
Source: Commentary on Romans