Why is a holomorphic map between compact connected Riemann surfaces a branched covering?

I have seen it claimed that a non-constant holomorphic map $f:X \rightarrow Y$ between compact connected Riemann surfaces is a branched covering i.e. surjective and there is a finite set $\Sigma \subset Y$ and $r \in \mathbb{Z}_+$ such that $|f^{-1}(q)|=r$ for all $q \in Y \setminus \Sigma$. I can see why such a map is surjective, but I don't understand why the rest of the statement is true.

Let $A \subset X$ be the set of all $p \in X$ where $f$ is not locally injective, i.e. let $A$ be the set of $p \in X$ such that there exists no open neighbourhood $U$ of $p$ such that $f$ is injective on $U$. (People usually refer to $A$ as the set of branch points of $f$ in $X$.)
We can show that $A$ is a finite set. To do this, we observe that around any $p \in A$, we can find local coordinates in which $f$ is represented as the mapping $z \mapsto z^k$, with $k \geq 2$. This follows from the open mapping theorem of complex analysis. (Of course, the point $p$ is meant to be the point $z = 0$ in these local coordinates.) But having written $f$ in this way, it's clear that $f$ is not locally injective at any point in this little coordinate patch around $p$, except at $p$ itself. In other words, $p$ is an isolated point in $A$. Since $p$ was chosen arbitrarily, this means that every point in $A$ is isolated. By a compactness argument, it follows that $A$ is a finite set.
Now define your $\Sigma$ to be $f(A)$. So $\Sigma$ is a finite set in $Y$, as required. Moreoever, $f$ is locally injective at any point in $f^{-1}(Y\backslash \Sigma)$, by construction, and therefore, by another application of the open mapping theorem, $f$ is locally a homeomorphism at every point in $f^{-1}(Y\backslash \Sigma)$. And clearly, the preimage of any point in $Y \backslash \Sigma$ is a finite set, by a compactness argument. From these facts, it should be simple to check that the restriction of $f$ to the subset $f^{-1}(Y\backslash \Sigma)$ is a covering map. Finally, $Y \backslash \Sigma$ is connected (because $\Sigma$ is finite), so $|f^{-1}(y)|$ is constant as $y$ varies over $f^{-1}(Y\backslash \Sigma)$: this constant is simply the number of sheets of the covering map.
First of all, your definition of a branched covering (between surfaces) is incomplete. The correct definition is: It is a map $f: X\to Y$ such that there exists a finite subset $W\subset Y$ such that for $Z:=f^{-1}(W)$, the restriction $$f|_{X - Z}: X- Z\to Y- W$$ is a covering map.
To prove that every nonconstant holomorphic map between compact connected Riemann surfaces is a branched covering, let $W\subset Y$ denote the set of critical values of $f$ (i.e. points $w\in Y$ such that there exists $z\in f^{-1}(w)$ so that $f'(z)=0$; such $z$ is called a critical point of $f$). Then observe that since $X$ is compact and $f$ is nonconstant, it has only finitely many critical points (otherwise, the set of critical points has an accumulation point in $X$ which is impossible). Now, the restriction $f|_{X- Z}$ is a local diffeomorphism. It is also a proper map, i.e. preimages of compact subsets in $Y -W$ are compact. (This follows easily from compactness of $X$.) Furthermore, by the maximum principle, nonconstant holomorphic maps are open. Since $X$ is compact, $f(X)$ is closed in $Y$; since $Y$ is connected, it follows that $f(X)=Y$. Thus, by the construction, $f|_{X- Z}: X- Z\to Y- W$ is surjective. Lastly, applying Ehresmann's "Stack of Records" theorem, we conclude that
$f|_{X- Z}: X- Z\to Y- W$ is a covering map.