You’ve not yet used the fact that $X$ is connected, and that’s essential. Otherwise, $X$ and $Y$ could both be the two-point discrete space, and $f$ could be the identity map; all of the hypotheses except connectedness of $X$ would be satisfied, but $f$ wouldn’t be constant.
Let’s go back to your finite cover $\{B_{x_1},B_{x_2},\dots,B_{x_N}\}$ of $X$, where $f$ is constant on each of the open balls $B_{x_k}$. Let $F=\{f(x_k):k=1,\dots,N\}$; $F$ is a finite subset of the metric space $Y$. In fact, $F$ is a discrete metric space with the metric inherited from $Y$, so you really have a continuous function $f:X\to F$. You also know that $X$ is connected. You probably know that compactness is preserved by continuous functions; what about connectedness? And what finite metric spaces are connected?
I’ve added a full proof of a more general result, but since I didn’t want to give the full solution to the homework problem right away, I’ve spoiler-protected it; mouseover to see it.
Added: It’s worth noting that the conclusion follows from much weaker assumptions: $X$ need not be compact, and $f$ need not be continuous. Let $\mathscr{U}$ be the collection of open subsets of $X$ on which $f$ is constant, so that in particular $B_x\in\mathscr{U}$ for all $x\in X$. Fix $x_0\in X$, let $y=f(x_0)$, and let $U_0=\bigcup\Big\{U\in\mathscr{U}:\forall x\in U\big[f(x)=y\big]\Big\}\;.$ If $U_0=X$, we’re done: $f$ is constant on $X$ with value $y$. If not, let $U_1=\bigcup\Big\{U\in\mathscr{U}:\exists x\in U\big[f(x)\ne y\big]\Big\}\;.$ Then $f(x)=y$ for every $x\in U_0$, and $f(x)\ne y$ for every $x\in U_1$, so $U_0\cap U_1=\varnothing$. On the other hand, it’s clear that $U_0\cup U_1=X$, and $U_0$ and $U_1$, being unions of open sets, are certainly open, so $\{U_0,U_1\}$ is a disconnection of $X$. This contradicts the connectedness of $X$ and shows that in fact we must have $U_0=X$ and hence $f$ constant on $X$.