>>96528900
Short answer is we don't know because it's never been dug into deep enough in hard canon.
Long answer with a lot of assumptions is that it seems to be based on what you need for your job and where is the best location for it. You don't need a place in San Francisco unless you're working there, so we mostly see people working for Starfleet Headquarters etc. You're not going to get kicked out if you grow up there (like the Siskos in New Orleans) but you'll have a hard time moving there just because you feel like it. Accommodations are based mainly on family need with a slight degree of preference for higher rank or position, but even bachelor accommodations will keep people comfortable instead of being Tokyo shoeboxes.
Though with the franchise being based mainly on Starfleet members, what we see on screen is skewed by the fact that they're the closest thing the Federation has to a military and are treated accordingly. In the real world if you're in the military, you get told where you're working and given your new home based on your family needs and your rank. You can apply for off-base housing but it's not guaranteed, especially in more remote areas. Starfleet is similar because it's Starfleet, so we don't see what the deal is with civilians.