As said in the commentaries, the first one comes from the Dirac formalism. Simply put, it deals with quantum states as vectors $\lvert a \rangle$ whose components contain the projections of the system into different eigenstates. For a system, the set of state eigenvectors $\lvert a_i \rangle$ must be linearly independent $\langle a_i \lvert a_j \rangle=\delta_{ij}$ which is the inner vector product, and is equivalent to $\int \psi_i \psi_j dx^3$. And then there is the transition or projection operator, which results from outer vector product $\lvert a \rangle\langle b \lvert$ and yields a matrix characterizing the transition between states. Finally if you have an operator $\hat A$, represented by a matrix, you can find its components like $\langle a \lvert \hat A \lvert b \rangle$, where is easy to see that when expressed in terms of its eigenvectors, all non-diagonal values are zero and the diagonal contains the eigen values ($\langle a_i \lvert \hat A \lvert a_j \rangle$ = 0, $\langle a_i \lvert \hat A \lvert a_i \rangle= a_i$)
Now the case of the atom is similar to the harmonic oscillator, and basically that of any system dealing with confined particles: discrete levels and discrete energy steps to change from one to the other. So this allows to write the Hamiltonian for this kind of systems like counting total energy: if level k is occupied, then add $E_k$ to the system energy. This is what the atomic Hamiltonian represents: if you have the system in a state $\lvert \psi \rangle = c_g \lvert g \rangle + c_e \lvert e \rangle$ you will get $\langle \psi \lvert H^A\lvert \psi \rangle = (c_g^* \langle g \lvert + c_e^* \langle e \lvert) (\hbar \omega_g \lvert g \rangle \langle g \lvert+ \hbar \omega_e \lvert e \rangle \langle e \lvert)(c_g \lvert g \rangle + c_e \lvert e \rangle)$ which you will see to get the mean values for the energy of the system in this state with $p_e=\sqrt{c_e c_e^*}$ and $p_g=\sqrt{c_g c_g^*}$ are the probabilities for each state, or the projections of $\lvert \psi \rangle$ into the eigenstates $\lvert e \rangle$ and $\lvert g \rangle$.
The second one is similar, but here there can be many photons in one state. So the total energy inside the cavity is a counting of how many photons are inside, and the creation and annihilation operators can be seen here for the harmonic oscillator (probably relevant here) and other examples.