David is assigned one of the best monikers in all of scripture. He is a person after "God's own heart." That's such a beautiful, succinct portrayal, and many stories from his life reinforce that idea. It starts when God sends his prophet Samuel to anoint a new king, and to the surprise of everyone there, picks David despite his youth and despite the way he spends his time (shepherding the flocks). God does this because he sees in David a heart like His.
The fortitude and faith in David's heart is reinforced in his well-known defeat of Goliath. The faithfulness of David continues during his service to an unstable king. We see it in his deep and abiding friendship to Jonathan. It's on display when he keeps his oath and practices the steadfast love of God by inviting Mephibosheth, the son Jonathan, to live under his protection and eat at his table.
Yet like all of us, David is a complex person. His meteoric rise gives way to waves of highs and lows. Accompanying his moral victories are notorious stories of moral defeat. Reliance on God is sometimes replaced with self-reliance. Confidence frequently gives way to questions and concerns and occasionally cries of doubt.
Best I can figure, in the end what makes David a person after the heart of God is that he is always turning to God, regardless of the situation. When he hoped for rescue, when he needed refuge, when he looked for victory, when he was soundly defeated, when he hoped to save his son, when he lost his son, when he needed to repent, and when he needed to vent, he turned to God. Perfection is not possible, no matter who we are. But always turning to God...that's something we can all aim for.