I'm trying to prove the above statement, and I had a look at this site.
This ends the proof with the following statement
the rank is the number of non-zero eigenvalues
But, this is what makes me confusing, because it's not all the case that rank of a matrix equals the number of non zero eigenvalues as far as I know. This shows a counter example of this.
Any comment about the rank of idempotent matrix would be grateful.
From $A^2=A$ you get that the only possible eigenvalues of $A$ are $0$ and $1$.
From the fact that $A^2=A$ you get that any vector $x$ can be written as $x=Ax + (I-A)x$ and that this representation is unique. In other words, the range of $A$ and the range of $I-A$ are complementary subspaces.
By the Rank-Nullity Theorem, $\operatorname{rank}A=\dim\ker (I-A)$. And $\ker (I-A)$ is precisely the eigenspace corresponding to the eigenvalue $1$. Similarly, $\operatorname{rank}(I-A)=\dim\ker A$.
If we let $e_1,\ldots,e_m$ be an ortonormal basis of $\ker A$ and $e_{m+1},\ldots,e_n$ an orthonormal basis of $\ker (I-A)$, then $$ \operatorname{Tr}(A)=\sum_k\langle Ae_k,e_k\rangle=\sum_{k=m+1}^n \langle Ae_k,e_k\rangle=\sum_{k=m+1}^n \langle e_k,e_k\rangle=\sum_{k=m+1}^n1=\operatorname{rank}(A). $$