Does "I" in Y = C + I + G + X - IM refer to the domestic investment? We know the national income identity: $Y = C + I + G + X - IM$.

From many resourses, $I$ refers to domestic investment. For example in this link: https://opentextbc.ca/principlesofeconomics/chapter/23-4-the-national-saving-and-investment-identity/

However, in Mankiw's book, it says that $I= I^d + I^f$, the summation of domestic investment + foreign investment, as shown in


And actually in later pages of Mankiw's book, "I" is referred to domestic investment at many places also.

What does "I" accurately refer to?
I see your confusion that could also apply to $C$ and $G$ because Mankiw starts from 
\begin{equation} Y= C^d+I^d+G^d+X~~~~~  (1) \end{equation} 

$C^d$, $I^d$, and $G^d$ have been subjected to the same ``bit of manipulation'', which is to add and substract the foreign part such that we get the same equality (1).

So the idea here is just that $I$, after the manipulation, is essentially equal to $I^d$, the domestic investment, because $I^f$, the foreign portion, has been substracted from $I$ and appears now in import expenditures ($IM$). In other words $I$, after manipulation, is reinterpreted as domestic investment. 

\begin{equation} Y= C + I +G +X -IM ~~~~~  (2) \end{equation} 

I think it safe to say that calling $I$ domestic investment is here an "abuse of language"! 

The most important is that identities (1) or (2) are consistent with the way we calculate GDP using total value added.