And what would make the party holding the fortified postion give that postion up to fight on a field? I saw that in Kingdom of Heaven where the Crusaders met Salah Adin's army outside Jerusalem.
There are a number of strategic and tactical reasons one would leave a fortified position and sally forth.
Raids on the siege camp (common).
The besieging force is (considered) weak enough that the siege can likely be broken by sallying forward (uncommon).
The besieged force is too numerous to be sustained for long during a protracted siege, or suffers too much under the siege (common).
There are more reasons sallies have been performed, and more often than not, these will overlap or there'll be multiple concerns in play, but for the sake of providing a comprehensive answer I'll focus on these reasons.
Number 1 was very common in both castle and city sieges, and the idea was to launch a surprise attack to harass the enemy. This could take many forms, from small-scale infiltration during the night to sabotage food supplies or siege equipment, to larger assaults that involved a good portion of the garrison (still reliant on the benefit of surprise) that aimed to really disrupt the enemy siege camp, killing troops caught unaware and destroying as much as possible. Raids such as these could be devastating to the besieger if done right, weakening or disheartening the enemy or stealing them of much needed supplies. They could also fail spectacularly, and a botched raid could cause a garrison to lose a lot of forces, only hastening their defeat. A big part of how successful a sally could be was how organized the besieger's tactics and investments were. A fresh, disorderly siege camp was more prone to raids from both outside and the garrison than a well-prepared and dug-in siege camp with investments on both sides.
Raids were also far more common against a more numerically even enemy. Containing a garrison is what a siege is all about in the first place, so when a garrison is half the size of the besieging force and especially when it concerns a city siege (with a much larger circumference), the besieger could have difficulty dealing with containment, stretching their troops too thin and leaving them vulnerable to a sudden, concentrated raid. An example of this can be seen in the siege of Nuremberg, where the Swedish army (which was vastly inferior on the field) retreated into the city, and then still proved difficult to contain by the superior Imperial force.
Number 2 is the logical extreme of this. When a besieging force is too small to properly besiege a castle or city, there is a decision for the garrison or army contained therein: either wait it out, or sally to try and break the siege lines to reinforce and resupply but not necessarily breaking the siege, or sally forth to completely break the siege and rout the besieger. The advantage of the latter is that it saves you a lot of time (which is especially important if it's an army that's holed up somewhere) and it overlaps with concerns for the land/people in general in that damages to the hinterland and city itself are minimized. The disadvantage is that it's much riskier to sally with the whole garrison and attempt to destroy the enemy than it is to keep on harassing and disrupting them. An inferior force would be hard-pressed to successfully cut off a castle/cities supplies entirely, so the safe bet would be to try and maintain any supply lines and wait it out. But this isn't always desirable. Moreover, it's highly uncommon for a mediocre force to even try and lay siege in the first place. Besieging comes with its own dangers to the besieger and any would-be besieger should anticipate possible reinforcements or armies sent to relief the siege. Showing up with an underequipped, poorly trained or numerically inferior army to besiege a fort leaves an army open to destruction from both sides.
Number 3 is a relatively common occurrence where there is either too little food or rampant disease in the besieged castle/city. Armies in virtually every time period and setting are great logistical affairs, and keeping them fed and watered is always a big concern. When a castle or city was besieged, excess population could be a big problem. A medieval castle might have a food store to last its garrison of 100 men for 5 years, but if 500 peasants and an army of 1000 men retreat into the castle and the siege starts, then suddenly the stores are going to last less than 4 months. Civilians were dealt with in varying ways; sometimes they were denied access when a siege was impending, or even evicted from a fortification to spare the food stores. Cities were generally not garrisoned to the full extent that was possible, but castles tended to be pretty small during the early-mid medieval period (later they became bigger, but still small relative to the army sizes), and were built so that a tiny garrison could effectively defend it. Adding 10 times the men to the garrison when a retreating army enters would not necessarily bolster the overal defensiveness, especially since an outright assault is unlikely in the first place! Another problem in cramped fortifications with poor water and food supply is that they tend to breed dysentery and all kinds of other nasty outbreaks. Overall, while retreating to a set of walls might spare an inferior army from being destroyed by a superior force, this can easily lead to only a temporary save under the wrong circumstances. So what could an army do when locked up in a city with only a small amount of dwindling supplies left? They could sally, and try to break out, or at least temporarily relieve the siege to resupply. Against a vastly superior force this might be very desperate, but against a more even-matched foe this can be a much better option than starving to death until the inevitable surrender. To again use Nuremberg as an example: the Swedish army retreated into the city with little supplies to bring along. After only a couple if weeks his men were hard-kept from pillaging the city as the supplies ran low. This situation was only worsened when they were reinforced by a second (almost equally large) detachment. After this they almost matched the Imperial army in numbers, and they sallied against them with the intent to break the siege, because the food supplies were too low to hold out much longer:
The King retreated again to Furth on the outskirts of Nuremberg. Wallenstein followed and built himself a strong encampment on a long ridge overlooking the little river Rednitz and threatening Gustavus’s position. On July 27th he had information that the King, outnumbered, and too weak to risk moving from his camp, had sent for almost the whole of his scattered armies from south and western Germany. There was, Wallenstein calculated, no fodder for horse or man, and the King would have to fight or starve. If he starved, that was an end of his army; if he fought, that too, in the relative positions of the troops, would be death.'
...
At Nuremberg the army, both man and beast, suffered horribly. A damp summer made the conditions worse, and the shortage of food and fresh water increased the epidemic diseases always prevalent in the camp. Men and horses were dying with terrifying rapidity; the cavalry alone had diminished by nearly three-quarters. On September i8th the King decided to abandon the position at whatever risk.^1
None of that is to say that what is shown in movies and shows is sensible though. Not everything kings and leaders of that time did was "sensible" with a 20/20 hindsight and modern+western mindset to begin with, but it can be mentioned Hollywood is very fond of having soldiers leave a very defensible position to sally forth in what would IRL be suicide and a complete waste. Things like garrisons leaving the walls of a castle completely undefended to go and fight the enemy on even ground. Or even small but painful-to-watch flaws like siege battles where the sensible notion of firing immediately at anyone foolish enough to enter into arrow range is forgone in favor of 'tension' and the ever present "Notch, draw, hold..... <waits a couple of minutes> loose!"..... I'll try not to rant; movies with historically-set battles are best enjoyed for what they did get right, not what they didn't.
u/LuxArdens has already written about a beseiged party attacking outside the fortifications. Armies also, occasionally, chose to fight outside fortifications before there was any siege. There were two main reasons for this. First, the armies facing a potential siege can have a window of opportunity to attack the potential besieger at better odds than would be available later. This could be due to multiple reasons: enemy reinforcements might be expected, the defender might know that their supplies are very low already and their strength will decline quickly during a siege, and the attacker can be more vulnerable before they have built field fortifications and deployed their artillery. One example of this is the Battle of the Plain of Abraham (1759), which led to the British capture of Quebec City. Of the options available to Montcalm, the French commander (abandon Quebec City to the British, wait behind the walls of the city for reinforcements (and then attack), be besieged (if reinforcements were not expected, this would be a rather questionable strategy), or fight immediately), he chose to fight immediately. His reasoning:
We cannot avoid action; the enemy is entrenching, he already has two pieces of cannon. If we give him time to establish himself, we shall never be able to attack him with the troops we have.
Note that fighting outside your fortifications like this gives a safety net: in case of defeat, there is a good chance of being able to retreat to the fortifications, and then proceed to be the defender in a siege. Indeed, this happened at Quebec City the following year, when the French attempted to recapture the city; this was the Battle of Sainte-Foy (1760). The British commander, Murray, felt he had too few troops to reliably defend the length of the city walls, and opted to attack. (He may have also been influenced by the poor condition of the British troops, described as "a poor pitiful handful of half starved scorbutic skeletons", which would not bode well for a long siege.)
The French won the battle (an event commemorated in Battlefields Park, which covers the area of both battles), and the British retreated behind the city walls. A siege began, and the deciding factor would be whose navy arrived first. As it happened, this was the British, and the British retained Quebec City. The defenders of the city in both 1759 and 1760 attempted similar strategies. Both lost the battle, but the British successfully defended the city despite their loss.
The battle between the Crusaders and Saladin in Kingdom of Heaven was different. This battle illustrates a second reason to not sit behind fortifications: initiative.
Saladin had assembled a large army for the conquest of Jerusalem, and the Crusaders had assembled a large army for its defence. Saladin judged that he was too weak to successfully take a strongly fortified position defended by that Crusader army, and tried to lure them out to fight him in the open. How did he do this? He besieged Tiberias, a Crusader possession, and successfully provoked an attempt to relieve it. Raymond of Tripoli - Tiberias in the movie, for he was the one who held Tiberias - reportedly argued that Tiberias (defended by his wife) should be abandoned, but was over-ruled, and the Crusaders marched to meet Saladin. While the Horns of Hattin was an unfortunate place to meet and fight Saladin, the plan was not ridiculous - if they waited and defended Jerusalem, Saladin could capture not just Tiberias, but perhaps other peripheral Crusader possessions and fortresses. The Crusaders could (a) sit safer behind fortifications, and abandon the initiative to Saladin, or (b) accept a higher level of risk, and attempt to defeat Saladin. Option (a) involves certain loss of lands and economic damage, but preserves the army to defend the most important Crusader lands. Option (b) involves greater risks for both sides - it also exposes Saladin to the risk of defeat.
In the event, the Crusader army was destroyed at Hattin (Raymond of Tripoli (Tiberias) was one of the few Crusader commanders to escape), and Jerusalem was left too weak to be defended. Why couldn't the Crusaders do what the British would do at Quebec in 1760, and retreat behind the walls of Jerusalem? They were too far - Hattin was about 90km from Jerusalem. Saladin had laid his trap well, luring the Crusader army to where there was no safe convenient retreat (or drinking water, either).
In other campaigns, some commanders chose to remain behind their fortifications, and lost the war as a result, with the invading army capturing city after city. There can be serious disadvantages to abandoning the initiative to an invader (see, e.g., the Mongol conquest of Khwarezmia).