Why was David so sure he would win against Goliath?

He wasn't.  He wasn't sure that he would win against Goliath.  It was never recorded in the bible that God told David to kill Goliath.  So this act was an act of faith for David -- a gesture of faith.  The stakes are very high.  If David loses, Israel would be under Philistine authority and yet he was willing to gamble that.  

What can we learn from here?

1. God honors gestures of faith as long as the heart is correct.  

This in particular, David has the right heart.  He was concerned about Israel and God's reputation.  He was not there for his personal glory.

Look at what David says on 1 Samuel 17:26, 45-47:

“What will be done for the man who kills this Philistine and removes this disgrace from Israel? Who is this uncircumcised Philistine that he should defy the armies of the living God?”

"You come against me with sword and spear and javelin, but I come against you in the name of the Lord Almighty... This day the Lord will deliver you into my hands, and I’ll strike you down and cut off your head... All those gathered here will know that it is not by sword or spear that the Lord saves; for the battle is the Lord’s, and he will give all of you into our hands.”

Another instance recorded where God was blessed with someone is found in Solomon, David's son.  God asked him what he wants, and he did not ask for wealth, he asked for wisdom, not for his own glory but so that he can do his role faithfully.  Look at how God responsded on 1 Kings 3:10-14:

Since you have asked for this and not for long life or wealth for yourself, nor have asked for the death of your enemies but for discernment in administering justice, I will do what you have asked. I will give you a wise and discerning heart, so that there will never have been anyone like you, nor will there ever be.

God was really blessed with Solomon's response.  In tagalog, nahabag ang Panginoon.

Indeed, this was an act of faith.  It was not a sure ball but God blessed and honored it because David's heart is correct.

2. David was more concerned about God's perception of him more than people's.

In comparison to King Saul, Saul was more interested in pleasing the people more than God.  There are many instances in Saul's life that this happended.  David on other hand, cared less about this.  

When David had an opportunity to kill Saul in a cave, he didn't in spite of all the clamor of the people following him.  This decision had a big repercussion as this decision would mean that his people would have to go in to hiding again if this decision goes south, or worse Saul would have him killed right then and there.

3. David has witnessed God’s hand and faithfulness as a shepherd.

This trust in God was already tested and not in theory. He experienced it while taking care of the flock.

In conclusion, David was not sure.  It was an act of faith, for him it was really bad that for forty days no one came up on the side of Israel.  It seems no one would ever dare. He did not disregard what would happen to his people. But it and he can't stand the taunting anymore and he cared more about God's reputation more than anything else.