It was said of David that he was a man after God's own heart, a statement that was made about no other in all of scripture. When David was anointed to be king after Saul, he was still a teenager and the least of his brothers.
1 Samuel 6-13 says, "When they came, he looked on Eliab and thought, ‘Surely the LORD’s anointed is before him.’ but the LORD said to Samuel, ‘Do not look on his appearance or on the height of his stature, because I have rejected him. For the LORD sees not as man sees: man looks on the outward appearance, but the LORD looks on the heart.’ Then Jesse called Abinadab and made him pass before Samuel. And he said, ‘Neither has the LORD chosen this one.’ Then Jesse made Shammah pass by. And he said, ‘Neither has the LORD chosen this one.’ And Jesse made seven of his sons pass before Samuel. And Samuel said to Jesse, ‘The LORD has not chosen these.’ Then Samuel said to Jesse, ‘Are all your sons here?’ And he said, ‘There remains yet the youngest, but behold, he is keeping the sheep.’ And Samuel said to Jesse, ‘Send and get him, for we will not sit down till he comes here.’ And he sent and brought him in. Now he was ruddy and had beautiful eyes and was handsome. And the LORD said, ‘Arise, anoint him, for this is he.’ Then Samuel took the horn of oil and anointed him in the midst of his brothers. And the Spirit of the LORD rushed upon David from that day forward. And Samuel rose up and went to Ramah."
No one, including his own father, thought he could possibly be the man God wanted for the job, but God knew David's heart and had been preparing him to be King even while David was in the fields with the sheep. David did mighty things for the Lord. Now this is surely the man we think of when we hear that verse about God looking on the heart, but it has other applications as well.
Jesus and the apostles warned us of false teachers and wolves in sheep's clothing, and we are told that we need to test the spirits to see if they are of God, because not everyone who says he speaks for God, really does. The Pharisees are a good example of a beautiful package on the outside: they looked the part of godly men, but on the inside their hearts were far from pure. Jesus said they were "white washed sepulchers full of dead men's bones" (Matthew 23:27). In Matthew 7:22-23 Jesus said, "Knowing the correct password—saying 'Master, Master,' for instance— isn't going to get you anywhere with me. What is required is serious obedience—doing what my Father wills. I can see it now—at the Final Judgment thousands strutting up to me and saying, 'Master, we preached the Message, we bashed the demons, our God-sponsored projects had everyone talking.' And do you know what I am going to say? 'You missed the boat. All you did was use me to make yourselves important. You don't impress me one bit. You're out of here.'” (The Message) There will be many on that day that will bring out their credentials as Sunday School teachers and missionaries, even preachers and elders, but God is not impressed by our credentials. Only the blood of Jesus can save us and declare us not guilty.
If a man gets caught breaking the law and is brought into court, he will be in no way pardoned by the judge because of all the good things he did or the other bad things he did not do. He is still guilty of the crime he committed, as we are still guilty of sin and all our good deeds will not save us. Keeping part of the law of God is not good enough. James 2:10 (NIV) says,"For whoever keeps the whole law and yet stumbles at just one point is guilty of breaking all of it." So let us examine ourselves to make sure we are in the faith, and let us not be fooled by the outward appearances of others. Even Satan himself masquerades as an angel of light. (2 Corinthians 7:14) So let us be alert and self controlled.