Let us consider the entire problem in Fock representation. If a quantum system is designated by, $|n_1, n_2, ..., n_M>$ where $n_i=$ the number pf particles in the $i^{\text th}$ state and $M$ is the total number of states. $\sum_i n_i=N=$ total number of particles.
Now, if $a_i$ and $a_i^{\dagger}$ are the annihilation and creation operators respectively, then for fermionic case, $$a_i|n_1, n_2, ..., n_M>={(-1)}^{\sum_k^{i-1} n_k}\sqrt {n_i}{|n_1,..., n_i-1, ..., n_M>}_F$$
$${a_i}^{\dagger}|n_1, n_2, ..., n_M>={(-1)}^{\sum_k^{i-1}n_k}\sqrt {n_i+1}{|n_1,..., n_i+1, ..., n_M>}_F$$
So, applying the number operator $N_i=a_i^{\dagger}a_i$ on a general quantum fermionic state like $|n_1, n_2, ..., n_M>_F$, we get, $$\begin{align}
a_i^{\dagger}a_i|n_1, n_2, ..., n_M>_F&=a_i^{\dagger}{(-1)}^{\sum_k^{i-1} n_k}\sqrt {n_i}{|n_1,..., n_i-1, ..., n_M>_F}\\
&=\sqrt {n_i}\times \sqrt {n_i-1+1}{|n_1,..., n_i, ..., n_M>}_F\\
&=n_i{|n_1,..., n_i, ..., n_M>}_F
\end{align}$$
So we see that the number operator when applied gives the number of particles in the $i^{\text {th}}$ state.
Now, for fermionic particles, there can be either $0$ or $1$ in any state(i.e., no two fermions can be in the same quantum state within a quantum system simultaneously)which follows from Pauli Exclusion Principle . This is valid for fermions which have half-integer spin and not for bosons which have integer spins. That is why $n_i=0$ or $1$ for fermions.
I hope this answers your question.