A big part of learning to play this game is learning to predict AI behavior, and to use these predictions to get your troops to do what you want. It is not that hard. I'll walk you through it.
1. The AI is starts out doing nothing. It looks for the nearest preferred target, and initiates a move command. If there are no preferred targets, they initiate a movecommand on that.
2. The AI finds the shortest route to the target, acting as if walls don't exist. If there is a wall in the way of this shortest path, it will search for other routes which avoid walls. If the shortest route which avoids walls more than ~8 distance longer than the route with walls, then they will go with the initial route (which goes through the wall) and attack the wall where it intersects before continuing to move towards the target. If the target is destroyed before this step is completed, they will go back to step 1 and start over.
3. The unit arrives at its target. Where it arrives depends a bit upon how the move command was issued. If rally issued the move command, then each unit will be given a slightly different position along the edge of a structure. If the move command was part of normal AI, then it will go to the closest edge to the place where it started out. Anyway, once it arrives, it starts attacking.
4. It will now delay a certain amount of time (dependent upon the unit type) before it attacks again, and then issue a new attack. If the target is destroyed by the attack, then it will go back to step 1. If the target is not destroyed, then it will repeat this step.
* Rally - Rally gives all units a new target, based upon the grid location selected. It is important that while Rally can interrupt steps 1-3, it CANNOT interrupt step 4. Units will always wait their attack cooldown and issue one final attack before responding to the rally if they were in the middle of attacking a structure. If a unit cannot finish its current cooldown/attack before a building is destroyed, then it will process the rally command in terms of rallying to a location rather than on the target... But, correspondingly, if a unit responds to the rally when a building is not yet destroyed, and the building is destroyed while on the way to the rally location, then the rallied troop will never go there, and will return to step 1 as normal.
The uninteruptability of step 4 presumably is there so you cannot rally to make your units quickly double-attack, getting more burst damage. Rally is supposed to be purely a movement/control buff.
Granted, archers work a little different than this, they have a limited radius for their preferred targets, as opposed to map-wide like raiders/knights, and it misses some of the complexity, considering I'm pretty sure that unit AI has an action queue for each unit, but this should cover the vast majority
Some consequences of this:
* If you are running an infantry-heavy army It is sometimes beneficial to rally a building past the buildings you really want destroyed. The first of your troops will run to it and destroy it before the rest can arrive. The stragglers will then turn, and quickly destroy all of their nearest buildings.
* Where you initially drop a unit effects not only what it will begin attacking first, but what it will attack after that. When you get lucky with exceedingly poor base construction, sometimes you can set a unit up to attack long sequences of buildings without having any defenses activate.