There have been thousands of posts on this subject before (little exaggeration
) and the intro articles cover it a bit. Try the search feature some time
Anywhoo, here is my take:
Library - no point in building Libraries in cities with very little commerce or commerce potential like your unit pumps. Exceptions maybe extra culture for the borders and Creative makes Libs so cheap it may be worth it to use as a whip overflow building.
University - see above. Key is picking those 6 cities to get them up asap for Oxford. After that, put them in any other high commerce cities or GP farms.
Forge - My rule on forge is at least 4 base hammers or more. It's really not worth it otherwise unless your are IND. Get these up in your production cities asap, of course.
Factory - Situational. Again, high prod cities or space parts cities
Granary - Most important building in the game and should be the first building in each city unless you need border pops.
Aquaduct - "Situational" can be applied to this one. I actually like the aqueduct a lot, but I think some would argue against it as a must build. However, I would say "situational" applies here. They are a tad expensive early, so I don't really build them unless I REALLY need them to grow. Happiness is generally more important early. You may not need them everywhere or at all if you have a lot of health resources and/or EXP.
Courthouse - Situational. My rule is at least 5 maintenance cost in the city. Generally, you don't need them in cities close to the cap. Only reason you might build them everywhere is if EPs are important to your strategy.
The bottom line is city specialization deals directly with when and where you build some of the buildings above with the exception of granary (always) and aqueduct/courthouse (need based).
Then I try to specialse the city depending on resources. (this is where I go drastically wrong) and bankrupt the civilization
Not sure if I quite follow your last statement. I think it's better to base your city specialization on the land as opposed to just resources (although technically resources make up part of said land and can factor into such decisions)