This requires you to lay siege to the settlement in question. You need to be at war with the defending town's faction, but you can provoke them easily by raiding some nearby villages. The second option is to attack a local town, either on your own or with the help from others in your faction. The downside is that it takes a really, really, long time to get a town this way. Eventually, your loyalty will be rewarded as your leader will give you a castle or town. Later, you can talk to the leader of the faction after doing a few tasks for them or fighting their enemies, to become one of their vassals. The easiest way to get your own settlement is to join one of the established kingdoms as a mercenary.
Work for a faction and get a settlement as a reward Towns are a little better than castles, in that you'll have more actions you can take while inside them, while castles are easier to defend during sieges. The surrounding villages contribute to your town or castle's overall wealth and prosperity, but are also vulnerable to being raided for supplies by enemy nobles or bandits. These are towns and castles which you can take over control of.
Taking over the settlements is rather difficult, but keeping control and developing them further is also a big challenge. This means managing the many settlements that you'll be conquering along the way. You're also be able to take on the role of ruler in your own kingdom. It's not all just battles in Mount And Blade 2 Bannerlord.