What is happening in this problem? I recently solved a problem chess.com and it is below. What I don't understand is that why black moved its rook to h8 (1...Rh8). I think that black's position would have been better if it was something like 1...Rf7. Is 1...Rh8 really the best move for black? If yes, why? If not, what is the best possible move, though black is about to lose?

[FEN "8/p6r/5R2/5N1k/5K1p/7P/8/8 w - - 0 1"]

1.Ra6 Rh8 2.Ng7#
After 1.Ra6, Black is in Zugzwang - the purpose of Ra6 is to prevent Black's a-pawn from moving. Now Black must move his rook.  If he moves it off the h file, then White plays Rh6#.  However, after 1... Rh8, White mates anyway with Ng7.

Black can hold (after a fashion) if his Rook can control g7 and h6.  After 1. Ra6, he will have to leave one of those squares undefended.

And to answer your question directly, Black has no good moves after Ra6. He's going to get mated on g7 or h6.  So Rh8 is as good as any.