Mechanically, it seems that clans without a fief are prioritized when deciding who gets what, and then after that, it goes by clan rank. That's how it seems to me.
But let's talk about why personally laying siege to and taking a castle doesn't automatically give you the castle, since so many people think this is ridiculous. I get why you'd think that, but the key to understanding this is quite simple: You have to step outside our modern, meritocratic mindset and step into the mindset of a feudal society.
When you join a faction, you swear an oath to your king. That king is your liege. Everything you do is now in his name, for his glory. It's quite a serious oath. Historically, violating that oath could easily be punished by death. It's true that in practice, many feudal lords served themselves first, but they had to at least maintain the appearance of acting in service to the crown. More importantly for this discussion... Laws and obligations were, regardless, tied to the king. In Bannerlord, when you take that oath, you are your king's subject for better or worse.
In other words, when you take a castle, you're taking it for your king. The castle belongs to your king until he sees fit to bestow it upon a subject. Why does he deserve it? Because he's the king. Your enemies are his enemies - and by extension, the enemies of all your peers. I know ingame it seems like fluff, but step into your character's shoes for a moment. Say you're walking down the street and a group of ten hooligans beats the daylights out of you and takes your money. Now imagine that there's a person who would respond to that by sending a literal army of thousands to hunt down and punish those hooligans and everyone who works with them, and would deploy portions of that army to protect you the next time it happens. That's literally what we're dealing with here.
As long as the king has the authority necessary to uphold that arrangement, he deserves what you take. Now, should he give you the castle you earned without any help? Maybe. That's for you to decide. If you think you earned the castle and he won't give it to you, you have two options: You can humbly accept it and let everyone know you acted for the glory of king and country, or you can become discontent. The latter happened in history! A lot, in fact. If it bugs you enough, you can always leave his service. If you feel you're not getting your due anywhere, you can always start your own kingdom and keep as much as you want. Just be careful you don't disgruntle your own vassals the way your former king upset you...
In short, when you swear fealty to a king, you act in his name, not your own. That's the essence of feudalism, and that's why the castle isn't yours, no matter how hard you fought for it. Your reward is glory and renown, which will eventually let your king know that you're indispensable and will earn you a seat at the table when lands are doled out.