Let $A$ be a generic observable, that is, a self-adjoint operator on a Hilbert space $\mathcal H$. In general, there might be many values $\lambda$ in the spectrum of $A$ which do not have a corresponding eigenvector in $\mathcal H$. However, physicists often still choose to write the corresponding “eigenket” $|\lambda\rangle$, even if it's just a formal expression. For example, one often sees the integral:
\begin{equation}
\int f(\lambda) \; |\lambda\rangle\langle\lambda| \; \mathrm{d}\lambda
\tag{1}\label{int1}
\end{equation}
Although the individual symbols “$|\lambda\rangle$” and “$\mathrm{d}\lambda$” aren't well-defined, the integral as a whole surprisingly is!
It is a deep result in functional analysis, the so called Spectral theorem, that for every normal operator $A$ there is a (operator-valued) measure $E_A$ with these properties:
- If $\Omega$ is a subset of the spectrum of $A$, then $E_A(\Omega)$ is a projector to the corresponding “eigen-subspace”
- If $\Omega$ is the entire spectrum of $A$, then $E_A(\Omega) = I$
- If $\Omega$ and the spectrum of $A$ are disjoint, then $E_A(\Omega) = 0$
- The Lebesgue integral $\int_{\mathbb C} \lambda \, \mathrm{d}E_A(\lambda)$ is equal exactly to $A$
For every operator $A$ there is precisely one such $E_A$ and we call it the spectral measure of $A$.
If you take an “infinitesimal slice” of the spectrum $\Omega = [\lambda, \; \lambda + \mathrm{d}\lambda]$, then the measure will return a projector to that “infinitesimal eigenspace” $E_A(\Omega) = |\lambda\rangle\langle\lambda|$. Therefore it makes sense to identify the integral \eqref{int1} with the rigorously defined Lebesgue integral with measure $E_A$:
\begin{equation}
\int f(\lambda) \; |\lambda\rangle\langle\lambda| \; \mathrm{d}\lambda
\quad := \quad
\int_{\mathbb C} f(\lambda) \; \mathrm{d}E_A(\lambda)
\end{equation}
Now, it should be obvious from the property “2.” of the spectral measure, that the integral $\int |\lambda\rangle\langle\lambda|\mathrm{d}\lambda$ is equal to the identity for every normal operator. Your question is just a specific case of this integral, with $A=\hat x$. Since the math can be a little too abstract, you'd maybe want to see the spectral measure for $\hat x$ – well, in position representation it's just the characteristic function:
\begin{equation}
\big( E_{\hat x}(\Omega) \; \psi \big)(x)
= \chi_\Omega(x) \, \psi(x) =
\begin{cases}
\psi(x) \text{ for } x \in \Omega \\
\hspace{7pt} 0 \hspace{8pt} \text{ for } x \notin \Omega
\end{cases}
\end{equation}