Q. Jesus says near the end of the Sermon on the Mount, “Not everyone who says to me, ‘Lord, Lord,’ will enter the kingdom of heaven, but the one who does the will of my Father who is in heaven. On that day many will say to me, ‘Lord, Lord, did we not prophesy in your name, and cast out demons in your name, and do many mighty works in your name?’ And then will I declare to them, ‘I never knew you; depart from me, you workers of lawlessness.’”
The point of the passage seems to be that those who are rejected were trusting in their works since the justifications they bring to Jesus are the things they did in His name—they weren’t trusting in what He did for them. If that is truly the point, is it enough to simply understand intellectually what Jesus did and believe that we are saved by grace as an unearned gift ? It seems that the Bible kind of says that one is saved solely on an intellectual basis, but at the same time that one isn’t truly saved unless one demonstrates works as well. How do you read that?
That passage from the Sermon on the Mount might not be the best one to use in order to address your question, because at least as I see it, the issue there isn’t believing vs. works. The issue is trying to use Jesus’ name in exorcisms or prophecies as if it were some kind of magic word, without being a committed follower of Jesus personally–like the sons of Sceva we read about in Acts who were trying to cast out demons by saying, “In the name of the Jesus whom Paul preaches, I command you to come out.” (Note that the statement you quote comes in the section of the Sermon on the Mount that begins with Jesus saying, “Watch out for false prophets.”)
I think a better way to address your concern would be to compare and contrast the biblical perspectives you summarized at the end of your question: either that salvation comes by grace through faith, apart from works, or else that some kind of works are needed. For example, Paul writes in Ephesians, “For it is by grace you have been saved, through faith—and this is not from yourselves, it is the gift of God— not by works, so that no one can boast.” But James writes, seemingly contradictorily, that “a person is justified by works and not by faith alone.”
However, we can tell that Paul and James really aren’t contradicting one another by the way they both appeal to the biblical statement that says, “Abraham believed God, and it was credited to him as righteousness.”
In Romans Paul appeals to the fact that this was said of Abraham before he was circumcised. Thus he didn’t have to do any kind of works (such as being circumcised or anything else) in order to earn righteousness. His salvation was a gift that came by faith.
But James appeals to the very same statement to argue that we would never know that Abraham had genuinely saving faith unless he did something to demonstrate it. That’s why James says that “Abraham was justified by works when he offered his son Isaac on the altar”–not justified in the sense of being made righteous, but in the sense of being shown to be righteous. Only a person who was truly trusting God in faith would have obeyed such a difficult command.
And so if we claim to have been saved by trusting and believing in what Jesus did for us, we should reasonably expect that salvation to manifest itself in “works,” not things we do to earn or secure our salvation, but things that flow naturally from it: deeds of obedience, consecration, sacrifice, and service. These, both Paul and James would agree, are the signs of true faith.