This achievement is fairly easy to earn, but you will need to control a town or castle of your own before you’ll have the opportunity. At this point you will need to be in a situation where your town or castle is besieged by an enemy force, and you will need to repel their attack from the ramparts of your settlement. This means that if the enemy forces break through the main gates and infiltrate your castle or town, you will have failed the condition of “repelling an attack on the walls”.
You’ll need to make a strong defence of your settlement from the walls, and this is more easily done against smaller enemy forces if you can goad them into attacking one of your settlements. You can then focus on building a garrison which consists of elite archers, or position an army with such units in the settlement for the specific purpose of defending it from the walls. You can also position infantry and cavalry outside of the castle walls to increase the chances of repelling enemy forces before they infiltrate the settlement.