Verses 12 through 54 give us a detailed account of David's defeating of Goliath.
In verses 12 through 15 we have a word regarding David's status and occupation. His three older brothers went after Saul to the battle. David, the youngest of the eight sons of Jesse, was an attendant of Saul and a shepherd of his father's sheep.
David was sent by his father to strengthen his brothers with food and to greet them and to get some news about them (vv. 17-22).
David heard Goliath's defiance and found out that Saul would make the man who killed the defier rich and give him his daughter and make the house of his father free from tax and duty (vv. 23-27). David considered that Goliath was defying the armies of the living God (v. 26b). He also considered that to kill such a defier was to turn away the reproach from Israel (v. 26a).
Verses 28 through 30 tell us about the anger and the despising word of David's oldest brother, who mocked him, saying that he should have remained to take care of those few sheep in the wilderness. He condemned David by accusing him of having pride and evil in his heart. David responded by indicating that there was a cause for his coming there (v. 29). David realized that the cause was that he had been sent there by God to defeat the defier.
David gained the agreement of Saul for him to fight against Goliath (vv. 31-39). At first, Saul discouraged David from fighting with Goliath, saying that David was but a youth and that Goliath had been a man of war since his youth (v. 33). However, David had the assurance that Jehovah would deliver him from the hand of Goliath. David's assurance was based upon his experience of Jehovah delivering him from the paw of the lion and of the bear in his shepherding of his father's sheep. Because his experience as a shepherd had trained him to trust in the Lord, David could say to Saul, "Jehovah, who delivered me from the paw of the lion and from the paw of the bear, He will deliver me from the hand of this Philistine" (v. 37a). When Saul heard this, he said to David, "Go, and may Jehovah be with you" (v. 37b). Then Saul put his armor on him, but David tried it and took it off (vv. 38-39).