The formula
Damage = (base damage × attack% + luck) × attacker health / 10 × (200 − (defence% + terrain stars × defender health)) / 100.
Base damage comes from the damage chart. Luck adds 0 to 9% (some commanders change that). A unit at half health deals about half damage.
Counter-attacks
A defender that survives hits back if it is next to the attacker and can target it. Ranged units never counter. Hitting first, and hard, is how you win trades.
Use the tools
The game shows the expected damage before you fire. The Damage calculator and the Damage chart (under Help) answer any "what if".