When a major cigarette manufacturer, whose product has been shown to cause cancers and heart disease, contributes money to help public education, battered women, or the homeless, does that make everything alright?
Or when a crime boss sends flowers and arranges for an elaborate memorial and sends money to the widow of a man he had bumped off, does that make everything alright?
Or is it alright when a televangelist conducts healing and exorcisms in public while committing sexual abuse, misreporting income, cheating employees of pay, and having someone killed to keep them quiet?
Does doing things that help people override or excuse blatant, intentional harm? That's what it looks like we are talking about here, or actually, does doing acts of faith override or excuse a sinful lifestyle?
This passage in Matthew comes at the end of what is called the "Sermon on the Mount." In this section of Chapters 5-7, Jesus gives his disciples guidelines for living according to God's will. Just before this particular passage, in 7:15-20, Jesus warns his audience about false teacher--the wolves in sheep's clothing. Basically, Jesus tells them that a wolf can be dressed in a sheepskin, but its actions will give it away as a wolf, although he puts it in terms of a tree and its fruit. In the early church, false teachers did actually infiltrate the church, as we read in the books of 2 Peter and Jude. Peter writes about them:
"With eyes full of adultery, they never stop sinning; they seduce the unstable; they are experts in greed--an accursed brood." (II Pet. 2:14, NIV)
Maybe they were the same group referred to in the book of Romans that believed the grace of God and liberty in Christ gave them the license to do whatever their fleshly nature wanted. In any case, all the NT writers clearly point out that there is accountability of how to live--not according to ceremonial laws, but according to a love of God and others. The people Jesus rejected at the judgment apparently had no regard for the desire of God, or the real benefit of others in spite of what they claimed.
By the way, in this passage of Matthew, these people CLAIMED to have done miraculous works in the name of Jesus, but did they really? We don't know. Neither do we know their motives behind the works, but remember, earlier in Chap.6, Jesus states that some people do acts of righteousness for show--to gain praise from others, and he says that praise is the only reward they will get. So, we don't know the full story behind these people Jesus has rejected, but Jesus didn't reject people who committed to him if they slipped up. After all, Peter denied him three times, and Jesus didn't write him off.
Paul addressed this kind of issue his own way in I Cor. 13:1-3 when he wrote that even if someone could perform great acts of faith, without love this person really had nothing. So, it isn't being able to perform the acts of faith that one should value.