Most people who move to places like SF,NYC, and upcoming Seattle are career oriented people. Big law, big accounting, big tech, etc. The ceiling is higher than anywhere else in the country. Even if you pull just a few years for your resume, you can do anything anywhere in the world. I can speak more on SF because it's been my main hub for years.
The cost relates to the opportunities that you can't find anywhere else. Besides jobs, there are countless other perks. One is the sheer amount of connections you can make with other people. Same reason why anyone would get into a high ranking university, the connection opportunities. People with high academics, angel investors, foreign investors, start-up entrepreneurs, and basically people who have their mind together.
Here's a timeline of a typical software engineer. Graduate college. Get hired for around 90-120k a year with compensations adding up to 180k-200k a year. Room with other fresh graduates for 2-3 years. You get to save a lot of money during this time. Most companies here feed you lunch with breakfast and dinner at times. Free gym. They'll also send you on nice ski or camping trips every now and then. You don't have to spend much during this time.
Move out with significant other. Since this is SF, your SO isn't some houseperson with a social science degree that sits around on Tumblr all day so your house income is now almost doubled in most cases. On base salary alone your little family has 160k after taxes so if you follow the 1/3 rule you have 4.4k for a pretty posh high rise apartment that you can bike to-and-from work or take the bart. Another 3-4 years or so, after working hard you're a staff engineer. At my company that's 150k-250k base salary with the average at 200k. Again, that doesn't include compensation like stock options and cash bonuses which is huge around here, typically doubling your income. A lot of my staff engineers' family saves six figure a year. Think about that for a moment, that's significantly more than the country's median income.
This is all if you ONLY work. You can walk into any coffee shop in the financial district and there will be at least a dozen people working on their next start-up. Also remember how I spoke about the amount of connections and investors. The possibilities are limitless. But then again, most people just settle for average.