Casting spells takes uninterrupted focus - not only can the caster not be mentally distracted from the task at hand, but generally they’re more successful at castings spells when their gestures and incantations aren’t hindered. Combat, then, is a particularly tricky place to cast spells, as the odd arrow to the chest or troll clawing your face off - common hazards of adventuring, don’t you know - can be somewhat distracting. Fortunately any spellcasting adventurer worth their salt has trained to concentrate on their magical business, allowing them to deploy their arcane and divine payloads in spite of these nuisances.
When a caster in the midst of casting a spell is pestered, they must make a Concentration check to determine whether they keep their focus or not. Should they fail to do so, they’ll lose whatever spell they were casting - expending it as it if were cast, but without actually benefiting from the spell’s effects. A caster’s Concentration check is equal to their Caster Level and the Ability Score which modifies the spell of the class their casting. For example, Bards, Paladins and Sorcerers use Charisma, Clerics, Druids and Rangers use Wisdom, and Wizards use Intelligence (exceptions exist for class kits, of course). As with most checks, this Concentration check is added to the result of a d20 roll to determine success or failure.
Concentration Checks versus Injury
The two most common events in combat that require a Concentration check are Casting Defensively, or maintaining your focus while taking damage. In the latter case, the character must make a Concentration check whose DC is equal to 10 + damage dealt + spell level. If a goblin were to strike a Cleric casting Cure Light Wounds (1st-level spell) for five damage, the Cleric would need to succeed at a DC 16 Concentration check (10 + 5 + 1).
It’s probably safe to say that avoiding that damage in the first place would be more ideal than simply keeping your focus in spite of the pain. To that end, spellcasters will automatically attempt to Cast Defensively during combat, which will avoid provoking the usual Attack of Opportunity for casting a spell if successful. To do this, they’ll have to succeed at a Concentration (DC 15 + [spell level x2]) check. Failure means they provoke the Attack of Opportunity, as normal… which may necessitate another Concentration check if they take damage from said attack.