Lots of reasons, but some of the origins go back to World War II, when San Francisco was a deployment center for most of the military being sent into the Pacific arena. Gay men who reported for duty here were discharged very quickly rather than being shipped overseas. Over the years, the socially and politically progressive culture here has allowed the LGBT community to exist much more peacefully than in some other areas of the country, so many have migrated here to live relatively free of persecution.