XP gain is calculated by comparing your Force's Average Level (FAL) against the Enemy Unit Level (EUL) of each enemy destroyed/routed.

FAL is the average level of all of your forces participating in combat.
EUL is the level of each enemy unit, not the average of all of them.

You gain more XP from foes much stronger than you and less from foes much weaker than you.
You gain half as much XP towards levels as you do towards skills. See the following Table:
Your FAL vs their EULSkill XPLevel XP
You are over 2 level higher0.5 XP per destroyed/routed(Skill XP)/2
You are up to 2 levels higher1 XP per destroyed/routed(Skill XP)/2
They are over your level2 XP per destroyed/routed(Skill XP)/2

For example, your Level 2 Orcs and Level 2 Humans kill three Level 0 Goblins, two level 1 Elves, and one level 5 Human. Your FAL is 2.

Your FAL vs their EULSkill XPLevel XP
vs Gob - You're above 2 higher0.5 XP * 3 Gob = 1.5 XP(1.5 XP)/2 = 0.75 XP
vs Elf - You're within 1 level1 XP   * 2 Elf = 2 XP(2 XP)/2   = 1 XP
vs Hum - They are higher2 XP   * 1 Hum = 1.5 XP(2 XP)/2 = 1  XP

Your forces will have a total of 6 Skill XP and 3 Level XP to distribute.