From linear algebra, if $v_1, \dots, v_n$ is a basis of the vector space $V$ then every vector $v\in V$ can be written as a linear combination
$$
v = a_1 v_1 + \dots + a_n v_n\tag1
$$
where the coefficients $a_k$ belong to the underlying scalar field. Moreover, if $V$ is an inner product space and $v_1, \dots, v_n$ is an orthonormal basis then the coefficients $a_1, \dots, a_n$ can be computed using the inner product as $a_k = \langle v, v_k\rangle$. This is easy to prove by acting with $\langle\,.,v_k\rangle$ on both sides of $(1)$.
Now, it turns out that the set of Hermitian matrices with complex entries is a real vector space with inner product defined as
$$
\langle A, B \rangle = \mathrm{tr} (A^\dagger B).\tag2
$$
Also, it is easy to check that $I, X, Y, Z$ are orthogonal with respect to the inner product $(2)$ and since the space of $2\times 2$ Hermitian matrices is four-dimensional the matrices form a basis. By normalizing we can turn it into an orthonormal basis $I/\sqrt{2}, X/\sqrt{2}, Y/\sqrt{2}, Z/\sqrt{2}$. Then, every $2\times 2$ Hermitian matrix $\rho$ can be written as
$$
\rho = a_I \frac{I}{\sqrt{2}} + a_X \frac{X}{\sqrt{2}} + a_Y \frac{Y}{\sqrt{2}} + a_Z \frac{Z}{\sqrt{2}}\tag3
$$
and
$$
a_I = \frac{1}{\sqrt{2}}\mathrm{tr}(\rho) \\
a_X = \frac{1}{\sqrt{2}}\mathrm{tr}(\rho X) \\
a_Y = \frac{1}{\sqrt{2}}\mathrm{tr}(\rho Y) \\
a_Z = \frac{1}{\sqrt{2}}\mathrm{tr}(\rho Z).
$$
Assuming $\rho$ is a state, then $a_I = \frac{1}{\sqrt{2}}$ because the trace of the density matrix representing the state $\rho$, denoted as $\mathrm{tr}(\rho)$, always equals 1. Finally, substituting into $(3)$, we get
$$
\rho = \frac{I + \mathrm{tr}(\rho X)X + \mathrm{tr}(\rho Y)Y + \mathrm{tr}(\rho Z)Z}{2}.
$$