How does Natsuki Subaru manage to stay mentally sane?
Note: This contains spoilers toward recent episodes.

The simple answer: He doesn't.

The more in depth one:
He keeps up the farce of not being 'insane' by convincing himself that what he is doing is for a good cause, to "save Emilia". It's stated multiple times throughout the series by Subaru himself, that everything he is doing is for "her sake".
This gives him the motivation to keep his sanity to a certain point, though even he slips up at times, for example, when he wakes up in the bed before Ram and Rem, he appears sporadic and flustered, showing signs of being 'not sane.'

As we have seen in recent episodes (14 through 17), he has broken down his sane demeanor and become... as I'd like to put it: A Sniveling Madman. This is especially the case when he has to watch Rem mutilated before him (episode 15), as well as the moment in which he is infuriated at Otto (episode 16/early 17).