First off all the goal when bluffing is not to always win the hand. The goal is to win it often enough to make a profit. Like if we bet 20$ on the river into a 30$, we need to win more than 40% of the time. So if we run into a royal flush or some other hand, the opponent would never fold, this does not mean, we did anything wrong. Times to avoid bluffing is:
When the opponent has shown a lot of strength
The opponent is a tight player, who raised preflop, bet large on the flop, bet large on the turn and bet large on the river. We have a busted draw, so we cant win at showdown, if we call. But even so this is not the time to find some fancy check-raise bluff, because chances are, the opponent actually has, what he is representing. And since we are playing a 5$ game, he is not going to make some amazing fold with an overpair or a set, just because there are now 3 cards to a straight.
If we cant tell a convincing story
If we took a passive or neutral action on the flop and turn, we really need to think about, which hands, we would do that with, that want to bet the river for value. And if the answer is "none", then we should probably not bluff. Could be a board runout like Jh9c2h-5s-Jd, where flop and turn got checked through. If we bet here, we are saying, we have a J or maybe a 9. But would those hands not have bet already? A good opponent will sniff this out and call us with any bluff catcher including decent A high hands.
If we have showdown value
We have AJ, missed the board completely, and flop and turn got checked. This is not a good time to bluff, because there is a decent chance, AJ high is the best hand and can win at showdown. But at the same time, if we bet, its unlikely, that many worse hands would call.