[proofplan]
We show that every fiber of $f$ is both open and closed in the [cofinite topology](/page/Cofinite%20Topology). The key topological fact is that an infinite cofinite space has no nonempty proper clopen subsets, because a nonempty [open set](/page/Open%20Set) has finite complement and a nonempty open complement would force the original set to be finite. After choosing one point $x_0\in X$, the fiber over $f(x_0)$ is nonempty, hence must be all of $X$, so every point has the same image.
[/proofplan]
[step:Show that every fiber of $f$ is clopen in $X$]
Fix $y\in Y$, and define the fiber
\begin{align*}
A_y:=f^{-1}(\{y\})=\{x\in X:f(x)=y\}.
\end{align*}
Since $Y$ has the [discrete topology](/page/Discrete%20Topology), the singleton $\{y\}$ is open in $Y$. By continuity of $f$, the set $A_y=f^{-1}(\{y\})$ is open in $X$.
Also, $Y\setminus\{y\}$ is open in the discrete topology on $Y$. Since
\begin{align*}
X\setminus A_y=f^{-1}(Y\setminus\{y\}),
\end{align*}
continuity of $f$ implies that $X\setminus A_y$ is open in $X$. Therefore $A_y$ is closed in $X$. Thus $A_y$ is clopen in $X$.
[guided]
Fix $y\in Y$. We want to understand the set of all points of $X$ that map to $y$, so define
\begin{align*}
A_y:=f^{-1}(\{y\})=\{x\in X:f(x)=y\}.
\end{align*}
Because $Y$ is equipped with the discrete topology $\mathcal{P}(Y)$, every subset of $Y$ is open. In particular, $\{y\}$ is open. Since $f:(X,\tau_{\mathrm{cof}})\to (Y,\mathcal{P}(Y))$ is continuous, the preimage of every open subset of $Y$ is open in $X$. Applying this to $\{y\}$ gives that $A_y=f^{-1}(\{y\})$ is open in $X$.
We also need $A_y$ to be closed. A subset of $X$ is closed exactly when its complement is open, so we examine $X\setminus A_y$. The complement of the singleton is $Y\setminus\{y\}$, and this set is open because $Y$ is discrete. The preimage identity
\begin{align*}
X\setminus A_y=f^{-1}(Y\setminus\{y\})
\end{align*}
holds by the definition of preimage: a point $x\in X$ lies outside $A_y$ exactly when $f(x)\neq y$, which is exactly when $f(x)\in Y\setminus\{y\}$. Continuity therefore implies that $X\setminus A_y$ is open in $X$. Hence $A_y$ is closed in $X$.
Combining the two conclusions, $A_y$ is both open and closed in $X$.
[/guided]
[/step]
[step:Prove that an infinite cofinite space has only trivial clopen subsets]
Let $U\subset X$ be clopen. We prove that $U=\varnothing$ or $U=X$.
Assume, for contradiction, that $U$ is nonempty and proper. Since $U$ is open and nonempty in the cofinite topology, its complement $X\setminus U$ is finite. Since $U$ is closed, its complement $X\setminus U$ is open. Since $U$ is proper, $X\setminus U$ is nonempty. Hence, by the definition of the cofinite topology applied to the nonempty open set $X\setminus U$, the complement of $X\setminus U$ is finite:
\begin{align*}
X\setminus (X\setminus U)=U.
\end{align*}
Thus both $U$ and $X\setminus U$ are finite. Since
\begin{align*}
X=U\cup (X\setminus U),
\end{align*}
the set $X$ is finite, contradicting the hypothesis that $X$ is infinite. Therefore no nonempty proper clopen subset of $X$ exists.
[/step]
[step:Apply the clopen dichotomy to one nonempty fiber]
Since $X$ is infinite, $X$ is nonempty. Choose $x_0\in X$, and define
\begin{align*}
y_0:=f(x_0)\in Y.
\end{align*}
By the first step, the fiber
\begin{align*}
A_{y_0}:=f^{-1}(\{y_0\})
\end{align*}
is clopen in $X$. It is nonempty because $x_0\in A_{y_0}$. By the clopen dichotomy proved in the second step, $A_{y_0}=X$.
Therefore, for every $x\in X$, we have $x\in A_{y_0}$, and hence $f(x)=y_0$. This proves that $f$ is constant.
[/step]