Damage is based on the attack of the attacking unit, the defense of the defending unit, any terrain bonuses, any unit attack/defense bonuses, and luck.
When it is your turn to attack, and you scroll over an enemy unit to attack, there will be a range of damages that your unit will hit. This is based on the full health attacking power of the unit. If your unit is damaged, it deals less damage than if it were at full health. Different units have different strengths and weaknesses against other unit types, and so that factors in to how much damage is done.
Terrain bonuses help reduce the damage taken. Certain units gain attack bonuses on special terrains, and others gain defense bonuses. To learn more be sure to check out the guides in the guides section of the forum.
Finally, luck plays a part in how much damage is done. It is not always consistent, and because of the range of damages, it can be any of the damages in between.