"He asked me how long she should have stayed".
This is reported speech, that is, a sentence saying what someone else has said. Here is the same sentence in direct speech (like a conversation).
He asked me, "How long should she have stayed?"
Now, let's go back to that time when he asked this question. Pretend we are at that time in the past. His friend was not feeling well and she was staying in his house as a guest. She stayed for a week, and she just left. We are talking about this.
He asks me, "How long should she have stayed?"
I answer, "She should have stayed another week. She needed another week to rest and get better."
OK, let's go back to right now. Here is the conversation in reported speech:
He asked me how long she should have stayed, and I answered that she should have stayed another week.