We who are Jews by birth and not sinful Gentiles know that a person is not justified by the works of law, but by faith in Jesus Christ. So we, too, have put our faith in Christ Jesus that we may be justified by faith in Christ and not by works of the law, because by works of the law no one will be justified. (Galatians 2:15-16)
What exactly does Paul mean by “the works of law?” Note that the phrase is used three different times in the text. I think you can make a good case for understanding its use to refer back to the “Jewish customs” he had confronted Peter about (i.e., the Jewish dietary laws that caused Peter to back away from table fellowship with the Gentiles at Antioch – 2:11-14). Paul’s point is that as enlightened Jews both he and Peter understood that these laws couldn’t (and weren’t meant to) bring justification—that only came through Christ who is the One the law pointed to (Romans 10:4). That’s why they put their faith in Him rather than the Torah. By these laws “no one will be justified” (v. 16).

What exactly does Paul mean by this statement that most think is an allusion to Psalm 143:2 (“no one living is righteous before you”)? Taken as a whole, the Torah was more than law demanding obedience—it contained grace and forgiveness (i.e., the sacrificial system, Day of Atonement, etc.). Does he mean to say (as does the Hebrew writer) that all forgiveness ultimately flows through the cross? That’s possible, but I think he’s headed in a slightly different direction that deals specifically with the nature of the situation in the Galatian churches.
The Torah provided Israel with their national identity. In Romans 9:4 Paul catalogs Jewish blessings as being their adoption to sonship (their status as most favored nation/Hosea 11:1), the divine glory, the covenants, the receiving of the law, the temple worship, and the blessings. The place where a person could learn about all of these was the Torah. That being so, you can see how the Torah could become synonymous with Jewish nationalism since conforming to it (especially those provisions like circumcision or the dietary provisions) reinforced their isolation from the world around them. For Paul to speak against the Torah (as he does in Galatians) is to speak against the nationalism it was being used to support.
The Torah isn’t being pushed upon Gentiles so they can work their way to heaven. No, entrance into the covenant (through circumcision-5:2ff) is being presented as part of the “full” gospel (something Paul’s enemies accused him of failing to preach). In order to receive the blessing promised to Abraham and his seed, they have to come into Christ and Abraham. Their Torah reliance is part of the bigger picture of trusting in being a Jewish person (descendant of Abraham). It ought to be pretty clear that on the whole, Israel didn’t have any reason to trust in what they were doing. But they did trust in who they were and when Paul speaks of relying on the law (2:15-16, 3:10-11, 5:4), it is in this context rather than a legalistic one we should understand him.