Caveat: The first part of this answer takes a very technical stance on the BRST procedure, and additionally works with a finite-dimensional phase space for convenience. It could appear quite far from the understanding of ghosts in the average application of BRST transformations or ghosts as a tool.
The general conception of ghosts
There are many different levels at which one may discuss the appearance of ghosts, anti-ghosts, and their numbers in constrained Hamiltonian mechanics (which is the same as gauge theories on a Lagrangian level). One of them is partly sketched in this answer of mine, where the BRST operator is exhibited as the differential in the gauge Lie algebra cohomology.
We will look at a slightly different way of looking at ghosts, namely by "extending the phase space", in this answer, although this can be seen as a rephrasing of the Lie algebra cohomology approach in "phase space terms":
The BRST formalism, on an abstract level, seeks to implement the reduction to a constraint surface $\Sigma$ in a phase space $X$ not by solving the constraints $G_a$, but by searching a suitable enlargement of the phase space such that the functions on the enlarged phase space have a graded derivation $\delta$ living on them whose homology computes the functions on the constraint surface, which are the gauge-invariant observables.1
The enlarged phase space is obtained as follows:
A function on the constraint surface $\Sigma$ is given by the quotient of all phase space functions modulo the functions vanishing on the surface. Every function $f$ vanishing on the surface is given by
$$ f = f^a G_a$$
where the $f^a$ are arbitrary phase space functions. If one introduces as many variables $P_a$ as there are constraints, and defines $\delta P_a = G_a$ as well as $\delta z = 0$ for any original phase space variable, then the image of $\delta$ is exactly all functions that vanish on $\Sigma$. For $\delta$ to be graded, $P_a$ has to be taken to be of degree $1$. The degree of a function as simply the degree of it as a polynomial in the $P_a$ is called the anti-ghost number.2
The $P_a$ are lonely, and they need conjugate variables. These are given by so-called longitudinal 1-forms on the constraint surface, where a longitudinal vector field on the constraint surface is one that is tangent to the gauge orbits. Their duals are 1-forms which are only defined on longitudinal vectors. It should be geometrically intuitive (and it is in fact true) that the longitudinal vector fields are precisely the fields generating the gauge transformations (they are again just another incarnation of the gauge Lie algebra). Therefore, there are as many basic longitudinal 1-forms $\eta^a$ as there are constraints, and as there are anti-ghosts $P_a$. Since there is the natural action $\eta^a(P_b) = \delta^a_b$ by definition of the dual, it is also natural to just define the Poisson bracket on an enlarged phase space with coordinates $(x^i,p_i,\eta^a,P_a)$ by
$$ [\eta^a,P_b] = \delta^a_b$$
so the pairs $(\eta^a,P_a)$ act as additional pairs of canonical variables. The derivation is extended to the $\eta$ simply by $\delta(\eta^a) = 0$. Functions on this enlarged phase space are now assigned a pure ghost number based on their degree in the $\eta$.
Given any function on the enlarged phase space, the ghost number is simply the pure ghost number minus the anti-ghost number.
The nice thing about the ghost number is that it is the charge of a certain generator - it is measured by the operator3
$$ \mathcal{G} := \mathrm{i}\eta^a P_a$$
which fulfills
$$ [f,\mathcal{G}] = \mathrm{i}\operatorname{gh}(f)f$$
for any function of definite ghost number. The ghost number is physically important because being a state of ghost number zero is, together with the condition of being BRST invariant, the necessary and sufficient condition of being a physical state.
Obtaining this condition, however, requires now obtaining the BRST differential by adding another differential $\mathrm{d}$ to $\delta$, and showing that the $\delta + \mathrm{d}$ gives, when "small perturbations" are added to it, the nilpotent operator required for the BRST formalism. (The derivation of this is very technical, and sometimes known as the "theorem of homological perturbation theory") Examining then again the actions of $\mathrm{d},\delta$, one finds that the gauge-invariant functions are precisely those invariant under the BRST operator with zero ghost number, so the quantum theory should also impose this restriction.
1"whose homology computes" is math speak for it being an operator $\delta$, where the gauge-invariant functions are precisely the functions with $\delta(f) = 0$ and where we identify $f$ and $g$ if there is an $h$ such that $\delta(h) = f - g$. Also, this gets a bit more complicated in the case of reducible constraints.
2In the case of irreducible constraints, this already correctly computes the gauge-invariant functions, and one could in principle stop here. However, it is unsatisfactory to have added the $P_a$, but not have suitably conjugate variables for them in the Hamiltonian formalism.
3This definition is the discrete, non-conformal analogon to the expression for $U$ that is written in the question.
Main reference: "Quantization of Gauge Systems" by Henneaux/Teitelboim
The specific case of $bc$-CFT
A general "$bc$-CFT", i.e. a 2D conformal field theory with ghost-like fields is given by the ghost action
$$ \frac{1}{2\pi}\left(b(z)\bar\partial c(z) + b(z)\partial c(z)\right)$$
when the fields $b$ and $c$ have conformal weights $h_b$ and $h_c = 1 - h_b$, respectively. Phase space functions with ghost number zero translate now to operators with conformal weight $1$ (since they have equal numbers of ghosts and anti-ghosts in them, and the weight behaves additively).
This shows that primary physical states (by the state-field correspondence of 2D CFTs) in such a theory must necessarily have conformal weight $1$. This is of importance in string theory, where a $bc$-CFT with $h_b = 2$ is naturally added to the $X$-CFT of the worldsheet fields. For a generic CFT, all possible primaries could, in principle, be physical states, but the BRST procedure forces ghost number zero states, i.e. fields with weight $1$, as the only allowed physical states.