Avoiding giving stalemate is just one of those things you have to learn, like castling and the en passant rule. Along with trying to get a draw by repetition, it gives a hope of rescuing something to a player who is down on material. Actually, it's quite surprising that you have been playing this long and not run into it before.
It's interesting to consider what alternative there could be to the stalemate rule: if you haven't checkmated your opponent, you clearly haven't won, but if he has no legal move, the game has to stop; neither player has won, so it must be a draw. One might that the rules could forbid you to make a move that would lead to stalemate, but what if you had no other legal moves yourself? - stalemate again!