This is indeed a confusing point. The root of the issue is that there are two different definitions for the stored energy. The first definition, which is usually used in vacuum, is
$$W=\frac{1}{2}\varepsilon_0 \int E^2 \mathrm{d}\mathbf{r}$$
and the second definition, which is usually used in the presence of dielectric, is
$$W = \frac{1}{2} \int (\mathbf{D}\cdot\mathbf{E}) \mathrm{d}\mathbf{r} =\frac{1}{2}\varepsilon_0 \int E^2 \mathrm{d}\mathbf{r} + \frac{1}{2} \int (\mathbf{P}\cdot\mathbf{E}) \mathrm{d}\mathbf{r}.$$
They are not equivalent in general. The first equation is the total energy required to assemble the entire charge configuration, free and bound, starting from vacuum. The latter equation is the total energy required to assemble just the free charge configuration, starting with the unpolarized linear dielectric already present. The second equation is what we more commonly mean by the "energy" as we normally only control the free charge while letting the dielectric respond as it pleases.
The key point to understanding the difference is that once the static charge configuration has been set up, we have a net charge distribution which is the sum of the free and bound charge. The $\mathbf{E}$ is the same as the $\mathbf{E}$ produced by this net charge distribution alone, as if it were in vacuum. The rest of the dielectric can be removed without changing it. In other words, $\mathbf{E}$ only knows about the net charge distribution and does not distinguish between free and bound charge. Therefore, we can see that the first definition doesn't include any information about whether dielectric is present. It gives the same result as long as the net charge distribution is identical. You can remove all dielectric and replace all the bound charge (that was previously there) with identical free charge and the result will be the same. So the additional term in the second definition is precisely the additional energy needed to polarize the dielectric.
Here is a concrete example. Consider two geometrically identical parallel-plate capacitors, one without dielectric and the other with dielectric, charged to the same voltage. The electric field $\mathbf{E}$ and the charge density $\sigma$ are clearly identical in both cases. What's different is the free charge density $\sigma_f$. The first capacitor has $\sigma_f = \sigma$ while the second capacitor has $\sigma_f = \varepsilon_r\sigma$. When we normally speak of the charge in a capacitor, we are talking about $\sigma_f$ and not $\sigma$. The obvious conclusion is that the capacitance and stored energy in the second capacitor is greater. If we used the first definition, we would have concluded that the stored energy was the same. Therefore, it is clear that the second definition is what we usually refer to as the energy. Moreover, it reduces to the first one in vacuum.
This is covered in section 4.4.3 of Griffiths' Introduction to Electrodynamics.