Why does the Bible contradict itself when Paul says we are not justified by works, but James says we are justified by works?
First, we must define what the writers mean by their use of the word, "Works." Paul and James actually use the word in a different way and in a different context than a cursory reading might reveal. In Galatians 2:16 Paul states, "Man is not justified by the works of the Law but through faith in Christ Jesus." Here, an in all of the Pauline passages about justification by faith and not works, Paul is referring to the, "Works of the law." This is a reference to the Mosaic Law of the Old Testament. God gave Israel the law as a guide to their national identity. When James refers to works he does not refer to the works of the law. Nowhere in James' epistle, when he refers to good works, does he do so in the context of the Mosaic Law. Let's break these differences down further.
Paul is referring to the ceremonial law, as in Galatians 2:11-21. In this passage, Peter began to hold himself back from the Gentiles and would not eat their food. "Prior to the coming of certain men from James, he used to eat with the Gentiles; but when they came, he began to withdraw and hold himself aloof, fearing the party of the circumcision" (Galatians 2:12). By doing so, Peter was obeying the Mosaic Law regarding foods, but he was violating the law of love in Christ. For this, Paul rebuked him. Laws regarding diet are part of what is known as the ceremonial law.
The ceremonial law included regulations about things like washings, food, attire, religious practice related to the temple and feasts, the Sabbath day, laws of animal sacrifice and so on. Paul says that no one can claim righteousness by observing these laws. "By the works of the Law no flesh will be justified" (Galatians 2:16).
The ceremonial laws were a foreshadowing of what was to come in Christ. Regarding food, Jesus is the bread of life (John 6:35). Regarding attire, we put on Christ (Galatians 3:27). Regarding religious practice, we practice the presence of Christ. Regarding the temple, we are the temple of the Holy Spirit (I Corinthians 3:16). Regarding feasts, we have the Lord's Supper and look forward to the marriage supper of the Lamb (Revelations 19:9). Regarding the Sabbath, we have entered into God's rest (Hebrews 4:10), who have come to Christ. Regarding sacrifice, there is only one sacrifice, that of Jesus, that brings us forgiveness. These things in the law were the foreshadowing, but Jesus is the substance.
How is this different from the words of James in 2:14-24? James explicitly states that, "Man is justified by works and not by faith alone" (2:24). Is there a contradiction with Paul? Not at all. Two different things are being referred to by Paul and James. Paul is referring to, "Works of the law," I.E., works of the Mosaic Law, whereby James is referring to "Works" in general but not works of the Mosaic Law. How do we know this? Because in all of the passages from James' letter, none of them refer to works of the law. In fact, James' examples of justifying works are taken from the period prior to the giving of the law (Abraham in James 2:24), or are taken from an example of a person who was not given the law (Rahab the Harlot in James 2:25). James is saying that our works demonstrate that our faith is rightly placed. We do not simply believe in Jesus and do nothing. Rather, we believe in Jesus and engage in good works which demonstrate that our faith is valid and properly placed. Our good works are the evidence of true faith in Jesus. Thus, faith and works operate together.
Paul is referring to an all together different kind of work. Paul is referring specifically to works of the Mosaic Law which are ceremonial in nature and have no eternal impact for salvation. Not eating pork will not give you eternal life. Sacrificing a lamb will not give you eternal life. In fact, even obeying the Ten Commandments will not give you eternal life. The Ten Commandments were not ceremonial, but were part of the moral law. But even keeping the moral law did not guarantee eternal life. Jesus noted to the rich young ruler that only following him would give eternal life (Matthew 19:16-30).
There is no contradiction between the teachings of Paul and James on works and the works of the law. The teaching of these men is complimentary to one another and gives us rounded understanding of the place that good works have in the lives of those who have true faith in Jesus.