Knowing when to quit a poker game is important but the vast majority of poker players do not leave poker games for the right reasons. What are the right reasons and when exactly should you leave? At the heart of this question is another, more fundamental question; why do you play poker? The reason I play is to win money but many play for a variety of psychological reasons. Some seek companionship; others seek a boost to their ego or to simply kill time. Others play because they enjoy the act of playing.
Some players set loss / win limits; if they lose x $ or win y $ they quit. There is nothing "wrong" with doing this per se but it expresses in my opinion a basic misunderstanding of some fundamental poker concepts. Ultimately the deciding factor should be your expected value: if your expectation of profit is negative you should quit. This is a subjective measure, which means there is no easy way to figure out when this point is, but it is none the less the correct approach.

(U.S. players welcome - 100% deposit bonus up to $500 - Reefer Poker Review)
Your expectation is derived from a number of factors, some variable and others constant (or semi constant). Your poker knowledge, for example is a constant (for our purposes, at any rate) while your level of tilt and the level of your game (A game, D game) are variable. The juiciness of the game is another crucial factor, as is your fatigue level.
You should leave any game where your expectation is negative but you should also leave any game where you are not playing your A game. The simplest way to do this is to leave any time you make a serious mistake; regardless of whether or not you win or lose based on that mistake. Any time you feel that you are on tilt, or make a play you would not usually make if you weren't on tilt / down x amount of money / hadn't just lost that last pot, it means you should leave the game immediately and come back an hour later or another day.
Another good reason to leave a game is if the game texture changes. Did the fish lose all their money? Did tighter, more aggressive players sit down? This is the perfect time to leave a game but do it with class, don't run for the cashier while the fish is still getting up from the table.