Citizenship is like a membership based system. Members pay into the system therefore there are benefits of being in the membership. The government limiting access to foreigners is akin to limiting access private property to outsiders.
The government is also in charge of protecting members of its club against violence against foreigners. So that is another reason why they might play a role in restricting people outside to accessing it’s territory.
Also when you have a welfare system you cannot really have open borders because refugees use your welfare system. Like free public schooling. Food stamps. Free public healthcare. Etc
Also because citizenship is the key to the voting system and laws are passed based on the voting system.