The OT law is not binding on the NT believer today. Colossians 2:14 states plainly that the law has been taken out of the way by the sacrifice of Christ.
14 blotting out the handwriting of ordinances that was against us,
which was contrary to us. He took it out of the way, nailing it to His
cross; (KJV21)
The OT law (which “was” a requirement for all people) was fulfilled by Christ and then Christ’s righteousness was given to all by His substitutionary sacrifice. Christ did what you and I could not do and that was to live a sinless life according to the Law of Moses and the Commandments. We who believe/trust in Christ have received his perfect sinless life and have been credited with already keeping the whole OT law and commandments. That is what Christ’s righteousness is all about.
So then, what about your specific references to the Sermon on the Mount (Matthew 5) and James 2? We need to understand the greater context to understand how these verses fit into the NT covenant of grace.
In the Sermon on the Mount, we need to remember that Christ was teaching the law. He taught not as the scribes and Pharisees but as someone with authority. In Matthew 5, Jesus elevated the requirements of the law to a level above the capability of any human. Jesus is putting attaining righteousness by your own deeds, out of reach by all human beings.
You quoted Matthew 5:20 (NIV):
20 For I tell you that unless your righteousness surpasses that of the
Pharisees and the teachers of the law, you will certainly not enter
the kingdom of heaven.
Here Jesus is stating plainly that unless a person can exceed the ‘righteousness” of the extreme religious zealots of the day, you can’t enter into the kingdom of heaven. So, if you are trying to attain heaven by physical means, ie trying to keep the law by your good deeds and acts of holiness, then you are destined to fail. Again, Christ is putting the OT law requirements out of reach of all humans. That’s why Jesus wanted people to come to Him for their salvation. Grace gives us (for free) what we don’t deserve!
If you look at the format of most of Christ’s teaching in Matthew 5-7, you will see it fits the pattern of “you have heard it said, but I tell you”. Here Jesus is stating what the law says (or what most people believe it says) and then He elevates the requirement to the thoughts and intent of the heart. It’s not about murder, it’s about being angry with your brother. It’s not about the adultery, it’s about having lustful thoughts. Again, He elevates the requirements of the OT law to a level unattainable for all humans for which one of us has not had a stray thought and as James states, “if you offend in one point, you are guilty of all.” (James 2:10)
The reason that Christ taught this way was to get the Pharisees to see that they were sinners. The Pharisees always denied their sin. They were the epitome of self righteousness. Christ’s Sermon on the Mount attacked that self righteousness and attempted to get even the most hardened heart to see if they measured up to the spirit of the law and not just the written letter.
In James chapter 2, we must understand that James is differentiating between the works of the flesh (ie works of the law) and the works of faith/grace. This is very important to understand the words you quoted.
In James chapter one, James contrasts this difference in statement “doer of the word”.
James 1:22 (YLT):
22 and become ye doers of the word, and not hearers only, deceiving
yourselves,
James is referencing Paul’s statement in Romans 2:13 (KJV):
11 For there is no respect of persons with God. 12 For as many as have
sinned without law shall also perish without law: and as many as have
sinned in the law shall be judged by the law; 13 For not the hearers
of the law are just before God, but the doers of the law shall be
justified.
In Romans 2, Paul makes the point that no one can be a doer of the law for all have sinned and fallen short of the glory of God. So, James quotes that same principal and purposefully changes the word “law” to “word”, an obvious reference to the gospel of grace.
James then, takes this understanding that no one can be a doer of the law and makes a dramatic case for all believers to be judged no longer by the OT Law of Moses and the Commandments but they are to be judged to a new standard, the Law of Liberty.
James graphically illustrates what that will look like. He says at the end of chapter 1, that we should not have faith in Christ while having “respect of persons” or showing partiality, for partiality comes by the OT law. He says pure religion is to visit the widow and the fatherless in their affliction. This is significant because the Pharisess and Scribes would never visit those people in their affliction because the Pharisees blamed the widow and the fatherless for their own condition. The Pharisees believed that that if something bad happened to you, then someone’s sin was responsible. (see story of blind man in John chapter 9). Again, this belief system was completely based on the standard of the OT law and judgments against that standard.
James then, continues to draw distinctions between the OT covenant of the Law and the NT covenant of grace. In chapter 2, he continues the teaching on partiality by the discussion about the rich man coming into the assembly. James says not to treat the rich man with more favor and the poor man with less favor, which are judgments based completely on physical works and accomplishments. He then states that we should operate by the “royal law” which is to love thy neighbor as yourself. This is followed up by the instruction to show mercy for mercy rejoices against judgment (ie, judgments about sin and behavior based on the OT Law of Moses).
James then makes the statement in verse 12-13 (KJV):
2 So speak ye, and so do, as they that shall be judged by the law of
liberty. 13 For he shall have judgment without mercy, that hath shewed
no mercy; and mercy rejoiceth against judgment.
Here James states that mercy (powered by the NT covenant of grace) and NOT by judgment (powered by the OT covenant of the law) is the preferable way to live. He states that we are to live by a new law, a law called the Law of Liberty.
James goes on to demonstrate what he means. He tells us to show our faith by our works. However, these are NOT works of holiness or good deeds based on the OT Law of Moses. Instead, he says these are the works of faith that are based on the “royal law” which is to love you neighbor as yourself. He then gives examples of these works of faith: Abraham and Rahab.
Abraham contemplated murder as he moved to fulfill God’s instructions and Rahab lied to save the spies that were sent from Joshua. The very pointed teaching here is that a person will be praised by God as being faithful and will receive imputed righteousness who fulfils God’s will without allowing the Law to dictate his/her actions. The true believer will believe God when He tells us that all sin has been atoned at the cross and those who trust in Him receive His imputed righteousness through the sacrifice of Christ alone.
So yes, works are the evidence of faith but we should clearly understand that these are actions apart from the commandments and the OT law.