Suppose is not connected. Then, where , and both and are open and nonempty. Define by . Now, is continuous if the preimage of open sets are open. So, let's look at the open sets of . They are . The preimage of the empty set is the empty set, which is open in . The preimage of is , which is open in . The preimage of is , which is open in , and the preimage of is , which is obviously open in . So, is continuous. You have already seen the proof that the composition of continuous functions is continuous. So, must be a continuous function. Hence, must be disjoint, nonempty, open subsets of whose union is . This implies is not connected.

Edit: Disjoint comes from the definition of a function. If they were not disjoint, then let . Then and implying is not even a function.

Nonempty comes from the fact that are nonempty subsets of , so and must be nonempty subsets of .

Open comes from continuity of .

The union because the preimage of the entire image of a function is the entire domain.