Towns are constantly at risk of being attacked. Whether it's the local Prince or random mercenaries, someone is always planning to acquire the contents of the town coffers.
You can postpone an attack by the Prince by pleasing him with gifts, deliveries and by selling him large quantities of stocked goods. The prince may also request a loan, which often means that he is planning an attack. He will attack eventually, no matter what you do.
Mecenaries attack at random, there is no way of predicting such a siege. You will receive a warning long before they arrive though, which may give you some time to prepare (occasionally such a warning will not result in a siege).
Preparing for the worst
Any town will be attacked at some point or another, so it's important to be prepared and build proper defenses beforehand.
To win a siege the town must employ a strong garrison of guards. Planning ahead in training guards is crucial, there will not be enough time to build an army when the siege starts (though it is possible to recruit a unit or two when they get killed during a siege).
Gate cannons and bombards are an important asset in town defense. They do not actively participate in combat, in fact they barely do anything noticeable. Their main purpose is distracting deadly trebuchet fire that would otherwise target valuable ranged units (soldiers just can't take that kind of damage). They target the trebuchets as soon as they get in range, though the siege is usually over before any of the war machines are destroyed. Cannon upgrade allows the turret to withstand significantly more damage before it can be destroyed.
When a Pitch Shoot is built, the guards will be able to fight the enemies that climb the town walls, by showering them with buckets of hot pitch. Pitch shoot will also throw hot pitch at the battering ram, killing the enemies that stand near it. There must be enough pitch in town for this to work.
High morale of the guards is one of the most important factors that decide the outcome of the siege. Soldiers will be happy if there is no shortage of beer in town, and when there are enough ranged units manning the walls. Unmotivated guards will be more likely to flee.
Sieges usually last 22-28 days. Successful defense can reduce that to 14-20 days. If the town guards successfully counter-attack as soon as the enemy approaches the gates, the siege will be over in as little as 12 days.
Amount of guards
City should have a full garrison to win a siege. The optimal balance of ranged/melee units in a garrison depends largely on the stage of the game.
Early-mid game (first/second wall)
Attacking armies are relatively small. Training only ranged units is a reasonable tactic early on, such garrison can usually destroy the entire enemy infantry. Defenders focus first on enemy ranged units, then eliminate the swordsmen without fear of return fire.
Tip: placing a couple of cheap buildings right outside the gate will often distract the attackers for a while, giving the defenders more time to shoot them. This will prolong the siege by a day or two, giving more time to train reinforcements or for an additional Council Meeting.
Late game (third wall)
Having just ranged units will not be enough at this point. 50/50% mix of melee and ranged units usually results in a successful defense. Training slightly less swordsmen in favor of more ranged units is a good option as well.
Archers, crossbowmen and musketeers attack primarily enemy swordsmen. If the enemy swordsmen are thinned down so that the town watch can defeat them, the rest of the enemy units flee as they cannot fight melee units. If the defending ranged units are eliminated before the number of enemy swordsmen is sufficiently reduced, the siege is lost (a lot of defending swordsmen will flee).