If $f: X \rightarrow Y$ is continuous, and $X$ is connected, then $f(X)$ is connected I'm trying to understand the proof of:

If $f: X \rightarrow Y$ is continuous, and $X$ is connected, then $f(X)$ is connected.

What are we trying to do in the following proof (are we proving the contrapositive or trying to prove by contradiction)?

Suppose that $f(X) = A \cup B$ is a separation. Let $C = f^{-1}(A), D = f^{-1}(B)$. Then $C,D \neq \varnothing, C \cap D = \varnothing$. Write $A = f(X) \cap U$, where $U \subset Y$ is open. Then $C = f^{-1}(A)=f^{-1}(U)$ is open since $f$ is continuous. Similarly, $D$ is open.

 A: 
Suppose that $f(X) = A \cup B$ is a separation. 

It is assumed here that $f(X)$ is not connected so that we can reach some contradiction. 

Let $C = f^{-1}(A), D = f^{-1}(B)$. Then $C,D \neq \varnothing, C \cap D = \varnothing$. $\tag{1}$

This comes from the assumption that $A\cup B$ is a separation. [Added:]Note also that in this step, we also have 
$$
X=C\cup D.
$$
Thus if we can prove that both $C$ and $D$ are open (in $X$), then we will have a desired contradiction.

Write $A = f(X) \cap U$, where $U \subset Y$ is open. Then $C = f^{-1}(A)=f^{-1}(U)$ is open since $f$ is continuous. Similarly, $D$ is open.  

In this step, we are trying to argue that both $C$ and $D$ are open. Thus this fact (both $C$ and $D$ being open) and (1) together imply that $X$ is not connected, which is a desired contradiction.
[Added:] The reason we can write $A$ as the proof puts is that $A$ is assumed to be open in $f(X)$ with the subspace topology. 

Let $P$ be  that "$f:X\to Y$ is a continuous map between two topological spaces $X$ and $Y$". Let $Q$ be that "$X$ is a connected topological space". Let $R$ be that $f(X)$ is connected. So you statement is 
$P$ and $Q$ implies $R$. (Or, if P and Q, then R.)
What we are trying to do in the proof in your question is that assuming $R$ is not true and $P$ is true,  prove that $Q$ is not true. 

The difference between the Contrapositive method and the Contradiction method is subtle. Let's examine how the two methods work when trying to prove "If P, Then Q".


*

*Method of Contradiction: Assume P and Not Q and prove some sort of contradiction.

*Method of Contrapositive: Assume Not Q and prove Not P.


The method of Contrapositive has the advantage that your goal is clear: Prove Not P. In the method of Contradiction, your goal is to prove a contradiction, but it is not always clear what the contradiction is going to be at the start.
See more for this in the question
Proof by contradiction vs Prove the contrapositive.
A: The common definition of connectedness in topology is that $X$ is connected if it is not disconnected, where disconnected means that $X$ is a non-trivial union of two disjoint open sets. Thus, in a sense, connectedness is defined as the absence of disconnectedness (in contract to the definition of path connectedness, which is more intuitive in that it assert the existence of a connecting entity between any two points). This is possibly the source of your confusion. So, think of restating the theorem in terms of disconnectedness: If $f\colon X\to Y$ is continuous then $f(X)$ being disconnected implies $X$ must be disconnected. Convince yourself that this is equivalent your statement of the result, and then that the proof is directly showing precisely that.
Comment: It is possible to define connectedness directly, by means of connecting entities between any two points. See here for details, and in particular a proof of the result you mention that does not use contrapositive or proof by contradiction.  
