The middle ages is a long, long period, with many different types of city walls, fortifications and castles and methods to bring them down. How the defenders of a castle act depends a lot on how the besiegers try to bring their fortifications down.
It was rare to do a direct assault against the walls of a castle, especially if not ruined in some way before the attack, but it did happen. If the attackers raised ladders to scale the walls or brought up siege towers, the defenders would do their best to topple these or in the case of siege towers set them on fire. Wooden poles with forks in the end could be used to push over ladders and similar devices could be uses to rock siege towers back and forth (often from both ends) until they were toppled over.
There is some evidence that the Byzantines used hand-helf or wall-mounted siphons to spray fire over attackers that tried to scale the walls of their cities and fortifications. Otherwise, thin clay pots filled with naphta and with a small wicker, used like a medieval molotov coctail was not uncommon in the middle east.
Pouring boiling water or hot sand from cauldrons over enemies milling at the foot of the wall or at the gate, trying to batter it down was common. Oil as in the trope was probably less common, as it is far more expensive than water or sand and will not do the job much better.
The enemy could try to batter the gates down with a ram and enter the castle that way. Many castles had a draw brigde, if not over a moat filled with water then to a raised bridge, making the gate be several meters above ground level. The defenders could collapse this bridge beforehand, making it impossible to access the gate for the enemy.
In such cases, the attacker could build a ramp to get over a wall (especially if partially or completely collapsed) or up to the gate to attack it. This would often be done under the cover of siege walls or siege tents - basically walls and gabled roofs mounted on wheels or lifted on the backs of the men protected by them and brought forward to protect the men working or a ram or archers or crossbowmen harassing defenders trying to attack the tents or walls from the ramparts. If the defenders had heavier weapons, such as ballistas or later in the era, cannons, they could target these protective walls and roofs with them. At Dunbar Castle 1338, the Scotiish defenders loyal to Robert the Bruce, led by Black Agnes, Countess of Dunbar, with great effort dragged a very large rock up the walls to drop it on top of the siege tent constructed by the attackers led by William Montagu, Earl of Sailsbury to house a battering ram, crushing it and the men under it.
If the attackers breached the gate by battering ram or other means, they would often find themselves in a killing zone in the gate house, with another gate at the other end, and murder holes to their sides and above them, through which they could be shot at and have procectiles, rocks and boiling water and sand poured over them.
Another option for the attackers was to dig a tunnel (called a mine) under the walls, pile it with lumber and set it alight. Once the tunnel collapsed as the wood burned away, it would bring the wall above it with it, partially or completely, creating a breach through which the castle could be assaulted. The defenders would listen for digging sounds and there are tales of cups of water set out to see the vibrations in the water indicating digging through the earth below. The water-filled moat was one of the foremost defences against this, as the moat woudl drain, filling the mine with water making it impossible to work, and both alert the defenders and make further mining impossible. If the defenders wanted to dig a mine under a moat they would either have to dig much deeper or they would have to drain the moat (often by damming up the source that filled it), which was time-consuming. Once the defenders were aware of the enemy mining against them, they could dig counter-mines to try to find the enemy mine, attack the labourers and collapse the mine before it had a chance to reach the wall, forcing the attackers to start over.
Other than that, the attackers could use mangonels, catapults, trebuchets and other siege weaponry to attack walls, gatehouses and towers in an attempt to bring them down through repepated, non-stop battering with heavy rocks and projectiles. It was hard for the defenders to do something about this - towards the end of the era, in the laye 1300s, cannons started to become a common replacement of other siege machinery and fortifications would have their own cannon mounted and both sides woudl duel, trying to hit each other's cannon. At Dunbar 1338, Black Agnes and her ladies in waiting went out to use their handkerchiefs to dust off their walls when English projectiles hit, mocking the English for shooting "dust" at them and raising the morale of her own men.
Another way was to negotiate, offering various boons to the commanders or men of the castle, enciting them to surrender or a betrayal to open a gate or similar. Threatening captive relatives in order to encite surrender was also common. Keeping morale in order and not letting untrustworthy men command or control important points of the castle was the most common way to defend against such tactics.
The last option for the attackers was to lay siege to the castle in an attempt to starve them out. This was rarely done on its own but often combined with one or several of the options above, as they took time. Trying to poison the water supply or casue disease in the castle by throwing in bodies of cattle, horses or even men that had died from disease could be done to speed this up. Many castles had several independent sources of water, especially in the middle east, where rain could not be counted on to reliably resupply the water sources. Castles often had a large supply of food - many castles were manors and the centre of the feudal production in the area and thus serving as granaries as well as fortifications as taxes were often paid in natural goods such as grain, live animals (chickens, pigs, cattle etc) or other food stuff.
Castles developed to become more and more defensible during the medieval era - from simple motte and bailies early in the era,mostly constructed from wood, to intricate and large stone creations with inner keeps and sets of defensive walls, labyrinth-like entrances and so on, forcing the attackers to go through the same type of defences several times.
Towards the end of the medieval era, cannons had started to make the high, straight and relatively thin walls of castles obselete. When Karl Knutsson (Bonde), King of Sweden invaded Danish Scania 1452 he brought around 70 "kärrebössor" ("cart shooters"), probably light cannons mounted on sleds and carts which were enough to take out the older castles and manors of Scania but could not take down the more modern and strong fortifications of Malmö and Helsingborg.