Then when he says, "But the scripture hath concluded all under sin," he shows that the Law is not only not opposed to grace but serves it.
First, he shows that the Law serves God's promises;
Secondly, how this service was made manifest in the case of the Jews (v. 23);
Thirdly, how the Gentiles even without the Law obtained the promises of God (v. 26).
With respect to the first it should be noted that in general the Law serves the promises of God in two ways. First, because it exposes sin: "For by the law is the knowledge of sin" (Rom 3:20). Secondly, because it reveals human infirmity, in ...the sense that man cannot avoid sin without grace which was not given by the Law. And just as these two things, namely, the knowledge of a disease and the infirmity of the patient is a great inducement to seek medical treatment, so the knowledge of sin and of one's impotency lead us to seek Christ. Thus, therefore, is the Law the servant of grace, inasmuch as it affords a knowledge of sin and actual experience of one's impotency. Hence he says, "the scripture," i.e., the written Law, "hath concluded," i.e., held the Jews enclosed, "under sin," i.e., showed them the sins they committed: "For I had not known concupiscence, if the Law did not say: Thou shalt not covet" (Rom 7:7). Again, "hath concluded," because with the coming of the Law they took occasion to sin: "For God hath concluded all in unbelief, that He may have mercy on all" (Rom 11:32). And all this in order that they would search for grace. Hence he says, "so that the promise," i.e., the promised grace, "might be given" not only to the Jews, but to all them that believe, because that grace was able to free from sin; and this grace is "by the faith of Jesus Christ."
Source: Commentary on Galatians