It is saved in the project you switched out of. So if you had 50 hammers invested in the Settler the game remembers that. However certain projects will have the hammers decay over time if you ignore them for long enough.
From observation, I think that it works like Civilization IV hammer decay. That is:
Units start to decay after 10 turns of inactivity. They lose 2% of their hammers rounded up each turn.
Buildings start to decay after 50 turns of inactivity. They lose 1% of their hammers rounded up each turn.
I'm not sure about Wonders.