I have tried to look at many answers to this question, since it is a rather common one, but with no satisfactory answer up until now.
The truth of the story is: Hermione was crying in the girls bathroom because of Ron's comments and didn't know about the loose troll, which is why Ron and Harry came to warn her.
The story Hermione tells: She read about trolls and thought she could take it on by herself. Harry and Ron probably saved her life.
What seems to be the ideal story: Simply leave out the part about crying because of Ron's comments, there are other obvious reasons why one might be in a bathroom.
Possible answers that could make sense:
- The bathroom she was in wasn't the closest one to the great hall, so she would have had to explain why she was in that particular bathroom. Apparently she has been there "all afternoon", so perhaps that's why she needed to make sure not to reveal that she has been crying because of Ron and instead say she went looking for the troll. But this would raise the question of why she assumes the teachers would know for how long she was already in the bathroom.
- Hermione assumed that Ron and Harry wanted to fight the troll. She didn't know they came to save her so she decided to take the blame instead.
Do any of these answers make sense? If not, I don't see any reason why she would lie.
A common answer to this question is "She wanted to shift the blame onto herself" but she wouldn't really accomplish that since Harry and Ron still disobeyed teachers orders in order to save Hermione regardless of her motives.