The first form you gave only holds for the simple harmonic oscillator; however, the second form holds both for the simple harmonic oscillator but also the more general use of creation and annihilation operators in quantum field theory.
Creation operators add a quanta to a system while anhillation operators remove a quanta, this then applies to the simple harmonic oscillator as we have quanta of energy $\hbar\omega$. But for quantum field theory these quanta are particles. If we use the basis $\left\{|n\rangle\right\}$ where $n$ is the number of quanta in the system then the action of the creation and anhillation operators is as follows:
$$\begin{align}&a|n\rangle=\sqrt{n}|n-1\rangle&\text{anhillation}&\tag{1}\\&a^\dagger|n\rangle=\sqrt{n+1}|n+1\rangle&\text{creation}&\end{align}$$
As $\left\{|n\rangle\right\}$ is a complete orthornomal basis then we can express the operators in this basis:
$$a\equiv\sum_{j,m}a_{jm}|j\rangle\langle m|$$
where $\left\{a_{jm}\right\}$ are the matrix elements in this basis. Post multiplying by $|n\rangle$ we get:
$$a|n\rangle=\sum_{j,m}a_{jm}|j\rangle\langle m|n\rangle$$
And as the basis is orthonormal then $\langle m|n\rangle\equiv\delta_{mn}$ is the Kronecker delta; thus, the sum becomes:
$$a|n\rangle=\sum_{j}a_{jn}|j\rangle\tag{2}$$
Next, by the coefficients of $\left\{|n\rangle\right\}$ in equations (1) and (2) we can see:
$$\begin{align}a_{jn}&=\cases{\sqrt{n},&j=n-1\\0,&otherwise}\\\implies a&=\sum_m\sqrt{m}|m-1\rangle\langle m|\end{align}$$
Note taking the adjoint of $a$ gives:
$$a^\dagger=\sum_m\sqrt{m}|m\rangle\langle m-1|=\sum_m\sqrt{m+1}|m+1\rangle\langle m|$$
Which is consistent with the definition of the creation operator above.
Finaly, we can take the product:
$$\begin{align}a^\dagger a&=\sum_l\sqrt{l}|l\rangle\langle l-1|\sum_m\sqrt{m}|m-1\rangle\langle m|\\&=\sum_{l,m}\sqrt{lm}|l\rangle\underbrace{\langle l-1|m-1\rangle}_{\equiv\delta_{\left(l-1\right),\left(m-1\right)}=\delta_{lm}}\langle m|\\&=\sum_{m}m|m\rangle\langle m|\end{align}$$
This result makes sense as we define the number operator $N\equiv a^\dagger a$ post multiplying by $|n\rangle$ gives the expected result:
$$N|n\rangle=a^\dagger a|n\rangle=\sum_{m}m|m\rangle\underbrace{\langle m|n\rangle}_{\equiv\delta_{mn}}=n|n\rangle$$
Aside on Basis Representations of Operators
Operators map from one vector space to another. Consider a general operator $B$ that maps from a vector space $V$ to the vector space $W$. Let $\left\{|n\rangle_v\right\}$ and $\left\{|n\rangle_w\right\}$ be complete bases of the vector spaces V and W respectively (they need not be number states as used in the answer, but we will use the same labelling conversion for ease).
As any vector can be decomposed into a linear combination of a complete basis then let:
$$|\phi\rangle\equiv\sum_n\alpha_n|n\rangle_v\quad\text{and}\quad|\psi\rangle\equiv B|\phi\rangle=\sum_m\beta_m|m\rangle_w$$
then clearly $\beta_m\left(\left\{\alpha_n\right\}\right)$ must each be some function of the coefficients $\left\{\alpha_n\right\}$. As we are interested in linear operators this implies that $\beta_m\left(\left\{\alpha_n\right\}\right)$ must be linear in $\left\{\alpha_n\right\}$. Thus, we can express $\beta_m\left(\left\{\alpha_n\right\}\right)$ as:
$$\beta_m\left(\left\{\alpha_n\right\}\right)=\sum_n b_{mn}\alpha_n$$
for some set of coefficients $\left\{b_{mn}\right\}$ (there is no constant term as we require $B0=0$).
Now if we impose the further restriction on both bases that they are orthonormal as well as complete then we can write $\alpha_n\equiv\,_v\langle n|\phi\rangle$ so:
$$\begin{align}\beta_m\left(\left\{\alpha_n\right\}\right)&=\sum_n b_{mn}\,_v\langle n|\phi\rangle\\\implies B|\phi\rangle&=\sum_{n,m}b_{mn}\,_v\langle n|\phi\rangle|m\rangle_w\tag{3}\\&=\sum_{n,m}b_{mn}|m\rangle_w\,_v\langle n|\phi\rangle\tag{4}\end{align}$$
where (4) follows from (3) because $_v\langle n|\phi\rangle$ is a scalar and can be moved past the ket $|m\rangle_w$. Finally, we can remove $|\phi\rangle$ from both sides of (4), you cannot normally "divide through" by a ket but because $|\phi\rangle$ is any ket in the vector space V then we can in this case, this gives:
$$B=\sum_{n,m}b_{mn}|m\rangle_w\,_v\langle n|\tag{5}$$
where $\left\{b_{mn}\right\}$ are called the matrix elements and $b_{mn}\equiv\,_w\langle m|B|n\rangle_v$ which can be shown by pre and post multiplying (5) by $_w\langle m|$ and $|n\rangle_v$ respectively.
In most situations, V and W are the same vector space so the subscripts are dropped. This is entirely equivalent to algebra with kets being column vectors, bras being row vectors and $B$ being a matrix with elements $\left\{b_{mn}\right\}$ - hence the name matrix elements.