The walls of a medieval castle or city were strong. They were defended by towers placed along the walls. The weakest place in the defence was where people had to come and go, that is the entrance through the walls. This was defended by two guard towers and some kind of very strong gate. The gate was often defended by a moat and drawbridge. But there were other lines of defence, too. The space between the guard towers had slits in the tower walls where people could shoot arrows at anyone who attacked. Then there were holes above through which boiling water could be poured. These were called murder holes. But they all only made sense if the attackers could be held in the space between the towers, and that was the purpose of the portcullis. This heavy iron grill was let down by ropes using a winding mechanism in the towers. It went in a channel cut into the guard tower walls. Why use a portcullis instead of a thick door? You could shoot at attackers through the portcullis, and you could see what they were up to.
Nowadays, military people would describe all of these features as 'defence in depth'.
The video below shows the walls of Conwy, Wales. It has a gate with guard towers and the remains of the slot where the portcullis was.