They eked out profits in the N64 and Gamecube generations despite generally poor sales by keeping unit costs down and managing good first party sales.
They basically won the lottery with the Wii - between the quick-fix appeal of Wii-Sports, a very friendly media narrative (portraying them as the plucky underdog) and some well timed first party exclusives in the first 2-3 years of its lifespan, they made an unfeasible amount of money, most of which they banked.
Since then, their luck has run out and their output has generally disappointed. From the last 2-3 years of the Wii's life-span, through the Wii-U's launch and into the present, their gaming activities have generally been loss-making (sometimes startlingly so). However, they've been doing a lot of currency trading on the side, which has meant that some years/quarters they have shown an overall profit based on that. Just as pre-bailout General Motors was sometimes described as a pension fund that made some cars on the side, Nintendo has somewhat become a currency-trading house that makes a few games on the side. For the time being, they aren't going anywhere (and the Amiibo range has given their gaming revenues a boost, though this looks set to be temporary).
The longer term future for the company is more clouded. They've been able to sit on the takings from the early part of the Wii generation for a long time, at least in part because they've been paying out very small dividends to shareholders. The longer term risk is that shareholders will start to wonder what exactly is going on with the management of a company that is sat on a huge pile of cash, but not particularly good at earning more of it.
Shareholder revolts are generally rare in Japan (which hasn't really had a true Eisner-moment yet), but there are already signs of discontent coming out of reports of their shareholder conference calls. Their hesitant move into the mobile gaming market is almost certainly a reluctant sop to shareholder pressure.