As a general rule, the gender (+ number) of the pronoun in the following sentence is determined by the gender of the noun it is referring to. However, there may be some deviations:
(1) Das Mädchen weint. Es ist traurig.
(2) Das Mädchen weint. Sie (?) ist traurig.
Variant (1) is correct in any case. Sometimes you can read sentences like in (2). Strictly speaking, this is not correct German; however, by some people this variant is preferred to make the pronoun match the sex of the girl.
(3) Die Stadt ist schön. Sie gefällt mir.
(4) Die Stadt ist schön. Es gefällt mir hier.
Example (3) is correct in any case. You could use the pronoun es as in example (4). However here es does not refer to the noun Stadt but to the general circumstances, an imaginary subject, if you will.
(5) Ich studiere an der HU. Sie ist eine tolle Uni.
(6) Ich studiere am KIT. Es ist eine tolle Uni.
Here, in (5), sie refers to the HU which has a female gender because it stands for Humboldt-Universität which is female. In example (6), KIT is neuter as it is short for Karlsruher Institut für Technologie and Institut is neuter. So you need the pronoun es. As you can see, not the "gender of the abstraction" is used but the intrinsic gender of the proper noun.
(7) Ich mag München. Es gefällt mir gut.
(8) Ich mag München. (Not:) Sie gefällt mir gut.
Variant (7) is correct. München has neuter gender, even if you don't recognise it in everyday speech. That München is a neuter noun can be seen, for example, if you describe a special epoch of Munich: Das München der 50er Jahre. There are some towns that are not neuter (e.g. der Wedding; a part of Berlin); but as a rule of thumb you can say that towns in general are neuter. So (7) is correct; (8) is incorrect, even if München is a Stadt, a female noun.
(9) In Frankfurt habe ich den Main gesehen. Er ist wunderschön.
(10) In Frankfurt habe ich die Oder gesehen. Sie ist wunderschön.
Main and Oder are both Flüsse and the noun Fluss is male. However, rivers have genders which need not be necessarily male. The river Main is male; the river Oder is female. So in case (9) you have to take the pronoun er and in case (10) you have to take the pronoun sie, even if the female Oder is a (male) Fluss.