I assume you know the general theorem that, using the axiom of choice, every set can be well ordered. Given that, I think you're asking how hard it is to actually define the well ordering. This is a natural question but it turns out that the answer may be unsatisfying.
First, of course, without the axiom of choice it's consistent with ZF set theory that there is no well ordering of the reals. So you can't just write down a formula of set theory akin to the quadratic formula that will "obviously" define a well ordering. Any formula that does define a well-ordering of the reals is going to require a nontrivial proof to verify that it's correct.
However, there is not even a formula that unequivocally defines a well ordering of the reals in ZFC.
The theorem of "Borel determinacy" implies that there is no well ordering of the reals whose graph is a Borel set. This is provable in ZFC. The stronger hypothesis of "projective determinacy" implies there is no well ordering of the reals definable by a formula in the projective hierarchy. This is consistent with ZFC but not provable in ZFC.
Worse, it's even consistent with ZFC that no formula in the language of set theory defines a well ordering of the reals (even though one exists). That is, there is a model of ZFC in which no formula defines a well ordering of the reals.
A set theorist could tell you more about these results. They are in the set theoretic literature but not in the undergraduate literature.
Here is a positive result. If you work in $L$ (that is, you assume the axiom of constructibility) then a specific formula is known that defines a well ordering of the reals in that context. However, the axiom of constructibility is not provable in ZFC (although it is consistent with ZFC), and the formula in question does not define a well ordering of the reals in arbitrary models of ZFC.
A second positive result, for relative definability. By looking at the standard proof of the well ordering principle (Zermelo's proof), we see that there is a single formula $\phi(x,y,z)$ in the language of set theory such that if we have any choice function $F$ on the powerset of the reals then the formula $\psi(x,y) = \phi(x,y,F)$ defines a well ordering of the reals, in any model of ZF that happens to have such a choice function. Informally, this says that the reason the usual proof can't explicitly construct a well ordering is because we can't explicitly construct the choice function that the proof takes as an input.
If $\lt$ is a well-ordering of $S$, then we may define the lexical ordering on the power set $P(S)$, by which $A\lt_{lex} B$, if and only if the $\lt$-least element of the symmetric difference $A\triangle B$ is in $B$ and not in $A$. That is, we look to the first place where the sets differ, and then put the set without this element before the set with this element. This is just like the order in a dictionary, hence the name, since two words in a dictionary are put in order by comparing the first letter on which they differ.
To see that this is a total order, we check first that it is transitive. If $A\lt_{lex}B\lt_{lex} C$, then the first difference between $A$ and $C$ must be in $C$, since either this occurs before the first difference between $A$ and $B$, in which case it is in $C$ and not in $B$ and hence in $C$ and not in $A$, or else it occurs at or after the first difference between $A$ and $B$, in which case it occurs exactly at this difference, which is in $C$ and not in $A$. The order it linear since any two sets do indeed have a least difference, since $\lt$ is a well-order.
Best Answer
By the well-ordering principle, $X$ can be well-ordered.
"$\Rightarrow$" For every $y\in Y$, consider $f^{-1}(y)\subset X$ which is a non-empty subset, because $f$ is surjective. Using the well-order it has a least element $x_y$. Now, define $g(y):=x_y$. Then $f(g(y))=f(x_y)=y$, so $f\circ g=id_Y$.
"$\Leftarrow$" $id_Y$ is surjective, so $f\circ g$ is surjective, so $f$ must be surjective. No need of the well ordering principle here.