It is interesting to read accounts in Scripture when men were doing things “for God” (or so they thought or claimed), but God made it known that it was not actually “for Him.” “Cain brought an offering…to the Lord…but He did not respect Cain and his offering” (Gen. 4:3-5). King Saul “felt compelled and offered a burnt offering,” but he was told, “You have done foolishly. You have not kept the commandment of the Lord” (1 Sam. 13:12-13). The Israelites praised and offered sacrifices to God, but He said, “I hate, I despise your feast days” (Amos 5:21-27). Similar expressions are found in the New Testament regarding those who may have thought or claimed they were serving God, but the Lord said they were most certainly not (cf. Mt. 15:3-9; Col. 2:23).
The most straightforward passage on this is Matthew 7:21-23 – “Not everyone who says to Me, ‘Lord, Lord,’ shall enter the kingdom of heaven, but he who does the will of My Father in heaven. Many will say to Me in that day, ‘Lord, Lord, have we not prophesied in Your name, cast out demons in Your name, and done many wonders in Your name?’ And then I will declare to them, I never knew you; depart from Me, you who practice lawlessness!’”