While there is no perfect answer to that question, the general rule of thumb is to only accept hits that favour you. For example, if you have 14, there is a good chance that you can improve your hand with a hit. If you have 17, you are more likely to bust.
On the other hand, if the dealer is showing good cards—say, a 10, it may also be to your benefit to hit, even if you are sitting at a risky number.