It just depend on the *definition* of the *number operator*. In a Hilbert space ${\cal H}$, one starts from the algebra $$[a,a^*]=1$$ for a pair of operators $a,a^*$ defined in some invariant domain $D\subset {\cal H}$ also assuming that there is a unique vector, in $D$, denoted by $|0\rangle$ such that $a|0\rangle = 0$.

Only exploitong (i) the commutation rules above, (ii) the definition of $|0\rangle$ and (iii) the fact that $a^*$ is the adjoint of $a$, at least when working in $D$, one easily sees that $D$ must contains an infinite** orthonormal** set of vectors of the form

$$|n\rangle := \frac{(a^*)^n}{\sqrt{n!}}|0\rangle \quad n=0,1,2,\ldots$$

$n$ here just denotes how many times $a^*$ acts on $|0\rangle$ in the formula above to produce $|n\rangle$ up to normalization coefficients.

From the given definition, it turns out that

$$a|n\rangle = \sqrt{n}|n-1\rangle \quad \mbox{and}\quad a^*|n\rangle = \sqrt{n+1}|n+1\rangle\:.$$

The meaning of that $n$ depends on physical context. In elementary QM, this machinery is used to compute the spectrum of the Hamiltonian operator of the harmonic oscillator. In that case $n$ denotes an eigenvalue and $E_n = \hbar\omega (n+ \frac{1}{2})$. In QFT there is a more sophisticated construction and, in fact $n_{p}$ denotes the number of particles with a certain value of the four momentum $p$ and there are operatores $a_p, a^*_p$ for each $p$. (This construction, in QFT, can be readapted to a generic state not necessarily with defined momentum and relies upon the notion of Fock-Hilbert space space.)

The number operator $N$ is just *defined* as the operator such that $$N|n\rangle = n|n\rangle$$ whatever is the meaning of $n$. As the vectors $|n\rangle$ form a Hilbert basis in ${\cal H}$ (or in a closed subspace which can be considered the true physical Hilbert space of the system), $N$ turns out to be self-adjoint with pure point spectrum and thus is a proper quantum observable whose (eigen)values are the numbers $n$.

Using the commutation rules of $a$ and $a^*$ as well as the definition of $|0>$ one easily sees that $$N|n\rangle = a^*a|n\rangle\:.$$

In fact $$a^*a|n\rangle = a^* \sqrt{n}|n-1\rangle = \sqrt{n} a^* |n-1\rangle = \sqrt{n}\sqrt{(n-1)+1}|(n-1)+1\rangle = n|n\rangle$$ $$= N|n\rangle\:.$$

Since the vectors $|n\rangle$ form a basis, essentially by linearity, we can equivalently write $$N = a^*a\:.$$