This passage has caused great confusion among many. They believe it is teaching a works salvation. But we know the Bible does not contradict itself and it says elsewhere in Romans,
But to him who does not work but believes on Him who justifies the ungodly, his faith is accounted for righteousness, (Romans 4:5 NKJV)
There are many who say they have faith. The question is what kind of faith? What they mean is they believe in God. Can mere belief in God save a person? The demons have their theology right and believe there is one God, implying that they believe in God. Are they saved?
You believe that there is one God. You do well. Even the demons believe—and tremble! (James 2:19 NKJV)
So the issue here is what kind of faith? Is it saving faith? Is it a faith that believes ON Him who justifies or is it faith that merely believes in God?
A person who has faith ON Him who justifies will produce works as a result of that faith. James is making the point that if works are not present in a person's life who claims to have faith in God, then that person does not have saving faith. Their faith is dead.
However, many get this twisted up and preach that the way to salvation is by doing works and so they set about to gain merit with God through their works of mercy, ritual, sacraments etc. This is wrong.
how much more shall the blood of Christ, who through the eternal Spirit offered Himself without spot to God, cleanse your conscience from dead works to serve the living God? (Hebrews 9:14 NKJV)
Works can never save us. Works are always an outgrowth of our faith in and on Him who justifies. It's faith that recognizes that only God can justify a sinner and our works are like filthy rags when used to gain merit with Him.
But we are all like an unclean thing, And all our righteousnesses are like filthy rags; We all fade as a leaf, And our iniquities, like the wind, Have taken us away. (Isaiah 64:6 NKJV)
Our works only matter to Him once He has saved us. Then He is pleased when we do our works to glorify Him, but they do not earn any merit for salvation. They are an outgrowth of the work of God in us.