$(\implies)$ We prove the contrapositive. Suppose $A \subseteq X$ is infinite, but $f(A)$ is finite. We aim to show $f$ is not continuous, or that $f$ is constant. If $f$ is not continuous, then we are done, so we assume $f$ is continuous, and aim to show $f$ is constant.
We then have $X \setminus f(A)$ is cofinite, and hence open. I claim that
$$A \subseteq X \setminus f^{-1}(X \setminus f(A)),$$
which implies that $X \setminus f^{-1}(X \setminus f(A))$ is infinite, hence $f^{-1}(X \setminus f(A))$ is not cofinite. But, since $f$ is continuous, it must be open, and the only open set that isn't cofinite is $\emptyset$. Note the claim is equivalent to
$$f^{-1}(X \setminus f(A)) \cap A = \emptyset.$$
If there were some $a$ in the left hand side, then $f(a) \in X \setminus f(A)$, while $a \in A$, so $f(a) \in f(A)$, which is a contradiction. Thus, the claim holds, and so
$$f^{-1}(X \setminus f(A)) = \emptyset.$$
Then, $X \setminus f(A)$ contains no points in the range of $f$, i.e. $f(A) = f(X)$, and so $f$ has a finite range. Let this range be $y_1, \ldots, y_n$ be the distinct points in this range.
Note that $f^{-1}\{y_i\}$ for $i = 1, \ldots, n$ form a family of pairwise disjoint subsets of $X$. It is therefore not possible for two such sets to be cofinite. But, $f^{-1}(y_i) = f^{-1}(\{y_i\} \cup (X \setminus f(X)))$ is the inverse image of a cofinite set, and hence open. So, all but one of these sets must be empty (and one must be non-empty and hence cofinite, since all points in $X$ must map somewhere). Thus, the range can only consist of a single point, i.e. $f$ is constant, as required.
$(\impliedby)$ Suppose the infinite set condition holds, and $\mathcal{U}$ is open. We wish to show that $f^{-1}(\mathcal{U})$ is open as well. If $\mathcal{U} = \emptyset$, then this is clearly true, so we assume that $X \setminus \mathcal{U}$ is finite.
Suppose $A = f^{-1}(X \setminus \mathcal{U})$ were infinite. Then $f(A)$ would be infinite as well. But, $f(A) \subseteq X \setminus \mathcal{U}$, a finite set, so this would be a contradiction. Thus, $A$ is finite, and $X \setminus A$, i.e. $X \setminus f^{-1}(X \setminus \mathcal{U})$ is cofinite. We just need to show:
$$X \setminus f^{-1}(X \setminus \mathcal{U}) \subseteq f^{-1}(\mathcal{U}),$$
which will show $f^{-1}(\mathcal{U})$ contains a cofinite set, and hence is cofinite. But, this follows easily from the fact that $f^{-1}(X \setminus \mathcal{U})$ and $f^{-1}(\mathcal{U})$ are disjoint, as the inverse images of disjoint sets. This completes the proof.