It doesn't seem to matter where I go, I always end up trying to find that city's best bookstore. Because of the number of really good bookstores in San Francisco, it's really tough to call one of them the best. But, if for nothing else but the history of the place, I seem to always wander back to City Lights.
Founded in 1953, City Lights is, of course, best known for the Beats. Jack Kerouac hung out here when he wasn't across the street at Vesuvio. Allen Ginsberg's "Howl", published by City Lights, set off a landmark battle over First Ammendment rights when co-founder Lawrence Ferlinghetti was arrested at the store for selling the poem. Supposedly, Ferlinghetti still wanders the stacks. I've never seem him, but I've also usually got my eyes plastered on the books.
Even if you're not a fan of the Kerouac and Ginsberg, City Lights is worth the trip. The vast variety of books alone will keep you wandering around for hours. On top of stocking just about everything, the employees know all about what's on the racks. If you have even a vague idea of what you're looking for, ask. Chances are, somebody's going to know exactly what you're looking for.
For more information, check out www.citylights.com. They've got a list of upcoming events and a great selection of recommended books.