Aggressive :They target the nearest Building they see
Smart : they target the most important building you have. "most important" seem to imply that limited-supplies turrets and refineries are targeted first.
In every case, they won't target an invisible building.
Once they have targeted a building, they will take the shortest route to the building, attack it, and once it's destroyed find another target. The shortest route don't take in account your building, but do take in account tangleweb (that slow them down), road (that speed them up).
They also take into account the danger posed by yours turrets, and try avoid them. The smallest turrets are never perceived as dangerous (that why you don't see titans avoid your turret before the moon), but Blast Cannon and heavy weapons will be avoided if possible.
The Scarecrow work as if it were a very dangerous turret ; consequently, titans will try their best to avoid them. It won't help if the scarecrow itself is targeted (because the titans is aggressive or smart).
The inner working of the Decoy is not known but it seem to make every titan in range target it instead of the regular target. It's not known if some titans (boss, for example) are immune.
The last thing that change their pathing - and one of the least understood - is collisions. Attacked titans are stunned for a little time (1/30th to 1/2 of a second) and consequently will slow down. Their brethen can't pass throught them, and will circle around the attacked titans. This can lead them to attack barricades or turrets, and it's in fact the main attack reason in the early game.