it can refer to just about anything in the previous sentence, and even to things that are merely implied by the previous sentence, for example:
Bob went to Costco to buy some bread. It was his last trip.
The writer/speaker is responsible for supplying sufficient context so that the reader/listener can tell what the pronoun represents.
Generally speaking, personal pronouns like it refer to something in the same or previous sentence but, if a paragraph contains several sentences, a pronoun can be repeated in every sentence to refer to something in the first sentence, though the thread might be lost if one sentence in the middle does not contain the pronoun.