What factors determine whether militia will assist in a battle?

When you right-click an enemy army to attack them, before you release the right mouse button, you get an overlay of where the battlefield will be.

This could be tactically important if you e.g. need to keep the high ground or wish to engage them from a direction that suits you best.

If a city falls in the designated battlefield tiles, its militia will join the fight.

As was mentioned in the comments, you need to have militia ready, that have not already fought this turn.