فَهَزَمُوْهُمْ بِاِذْنِ اللّٰهِ ۗوَقَتَلَ دَاوٗدُ جَالُوْتَ وَاٰتٰىهُ اللّٰهُ الْمُلْكَ وَالْحِكْمَةَ وَعَلَّمَهٗ مِمَّا يَشَاۤءُ ۗ وَلَوْلَا دَفْعُ اللّٰهِ النَّاسَ بَعْضَهُمْ بِبَعْضٍ لَّفَسَدَتِ الْاَرْضُ وَلٰكِنَّ اللّٰهَ ذُوْ فَضْلٍ عَلَى الْعٰلَمِيْنَ ( البقرة: ٢٥١ )
Fahazamoohum bi iznillaahi wa qatala Daawoodu jaaloota wa aataahul laahulmulka Wal Hikmata wa 'allamahoo mimmaa yashaaa'; wa law laa daf'ullaahin naasa ba'dahum biba'dil lafasadatil ardu wa laakinnal laaha zoo fadlin 'alal'aalameen (al-Baq̈arah 2:251)
So they defeated them by permission of Allah, and David killed Goliath, and Allah gave him the kingship and wisdom [i.e., prophethood] and taught him from that which He willed. And if it were not for Allah checking [some] people by means of others, the earth would have been corrupted, but Allah is the possessor of bounty for the worlds. (Al-Baqarah  : 251)
1 Mufti Taqi Usmani
So, they defeated them by the will of Allah, and Dāwūd (David) killed Jālūt, and Allah gave him the kingdom and the wisdom and taught him what He willed. Had Allah not been pushing back some people by means of others, the earth would have been spoiled. But Allah is All-Gracious to all the worlds.