A trifecta was originally, and still is, a type of bet, particularly a bet on a horse race, in which one tries to predict which horses would come in first, second, and third place. In order to win you have to predict all three horses correctly and in the right order. Needless to say, this is very hard to do, and the payoffs can be immense. This is a variation on the perfecta (or exacta), in which the bettor must predict only the first and second place winners.
In common parlance, trifecta has come to mean a perfect set of three, the three perfect examples of a given category. It has overtones of hitting the jackpot, although it is often used ironically or sarcastically, as when President Bush was quoted saying that he had "hit the trifecta" after hearing of the September 11th attacks; he had achieved the negative trifecta of war, recession, and national emergency.
As the term become more and more popular, any hat trick is likely to be called a trifecta, or in fact, anything involving the number three, for example a three point shot in basketball, or a menage à trois. It has also become a trendy name for sports clubs, sports bars, and anything else that thinks it can get away with it.
In the betting world a trifecta is surpassed by a superfecta, in which one must call the top four winners. In popular parlance, you may also hear of quadfectas and quinfectas (four and five exemplary items) although 'fourfecta' and 'fivefecta' are becoming more common as the masses dumb down our pretentious slang.