When you're down a few pieces or even in a bad position, which is better, playing offensive or defensive?
The first question to ask yourself when you are losing is "Is it time to resign yet?" Sometimes the best play is to resign. There are two good reasons for resigning: 1) Like when a boxer's trainer throws in the towel or the referee stops the fight - to save yourself from further punishment. 2) Out of respect for your opponent in the case where you have absolutely no hope and you are a masochist and so 1) doesn't apply.
Not being a masochist I generally only play on when I can come up with a plan that gives me some hope. Obviously the plan depends on the position on the board. You can't just plan to launch a wild sacrificial attack in all circumstances and expect it to have any chance of working.
Here are some standard situations and plans to go with them.
1) Situation: Opponent is a piece for one or two pawns up and he only has 2 or 3 pawns left himself.
Plan: try and exchange off all his pawns. If the major pieces (rooks and queens) are off the board I may even be able to sacrifice a minor piece for a pawn at the end to secure the draw.
2) Situation: Opponent has a difficult "book" win, say R v N which is separated from the king or B+N.
Plan: know what he needs to do to win and try and foil him. He many not know what to do. e.g. against B+N move my king towards the "wrong" corner for my opponent. in R v distant N try and reconnect my K and N.
3) Situation: Straightforward pawn down in an endgame where I would be confident of winning if colours were reversed.
Plan: Activate my king and attack on the side where he doesn't have a majority. If at all possible don't move pawns on the side of his majority. Don't just wait passively for him to carry out his plan.
4) Situation: Not very many available moves
Plan: Look for stalemate possibilities.
5) Situation: Lots of material down.
Plan: Look for a fortress (position where despite my material deficit I can block him from winning)
6) Situation: a couple of pawns down in an endgame where along with a few pawns we have a rook and opposite coloured bishops.
Plan: Try and get the rooks off. Then get my pawns on the same colour as my bishop so that it can defend them. Opposite coloured bishop endgames are very difficult to win. It is very difficult to move a passed pawn onto the colour of the opposite bishop.
As you can see from these examples there is no general all-purpose plan. What you need to do depends very much on the position. In one kind of position you need to get the pawns off, in another specific pieces off. In one position you need to attack to avoid being ground down, in another try and build a fortress and hunker down.
What you will also see is that the knowledge you need to implement these different plans is mostly endgame knowledge. Not for nothing do the experts advise that the most important part of the game to study for maximum benefit is the endgame.
Here's an example of 5 from one of my games. We are both down to about 2 minutes each on the clock with a 30 second increment. I'm winning and shortly will win the d pawn after which I can go after the g and h pawns. Unfortunately I blunder, take the d pawn too soon and suddenly I'm rook for queen down. I play on and spot a chance to go for a variation of the standard R+P v Q fortress. Fortunately my opponent is ignorant of this and lets me get the fortress. He demonstrates his ignorance of the fortress by playing on for about 20 moves after the game snippet below.
[White "Towers "] [Black "S.E.A 21/10/2013 - drawing with a fortress"] [FEN "8/5pk1/5n2/2Rp1q2/5Np1/4P1P1/5PK1/3R4 w - - 0 1"] 1.Nxd5 Qf3 2.Kh2 Qxd1 3.Nf4 Ne4 4.Nh5+ Kh7 5.Rf5 Qd7 6.Nf6 Nxf6 7.Rxf6 Kg7 8.Rf4 f5
I will look for an additional example of 3, again from my own games.
Usually when you are decisively down in material, the best option is to make the position as complicated as possible. If you don't create any threats, your opponent doesn't have a chance to go wrong. And if nothing special happens, you are going to lose. So stir it up, create a mess, chaos on the board and suddenly everything is possible again, especially if you're opponent tries to play it super safe and gets low on time.
If your position is just somewhat worse, however, lashing out with some sacrifices often makes it much easier for your opponent to finish you off. So dogged defence against all tactical possibilities is often a better try in such a situation.
Of course if your position is somewhere between worse and lost, it is often hard to decide which way to go. Miss too many chances to mess it up and suddenly you're dead lost. Mess it up to early and suddenly you get messed up. ;-)
One final tip: Try to be ahead on the clock. It is a very common scenario that you defend very well for a long time, and just when the position is equal again, you blunder the game away, because you're low on time.
An example by the current World Champ: Carlsen-Topalov, 2012: Incredibly Carlsen just overlooked 20…Nb2, as he said after the game, but he immediately sacrificed a lot more material to get an attack going. Objectively this shouldn't have been enough, but he created enough chaos to eventually confuse Topalov and in the end even win the game.
rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 1. e4 c5 2. Nf3 d6 3. Bb5+ Nd7 4. c3 Ngf6 5. Qe2 a6 6. Ba4 Qc7 7. O-O e5 8. d4 b5 9. Bc2 cxd4 10. cxd4 Be7 11. Nc3 O-O 12. Bg5 h6 13. Bh4 Bb7 14. Rad1 Rac8 15. Bb3 Rfe8 16. dxe5 dxe5 17. Bg3 Bf8 18. h3 Nb6 19. Nh4 Nc4 20. Nf5 Nxb2 21. Bh4 Nxd1 22. Bxf6 Nxc3 23. Qg4 Bxe4 24. Nxh6+ Kh7 25. Bxf7 Qxf7 26. Nxf7 gxf6 27. f4 Bg6 28. Qh4+ Kg7 29. fxe5 Ne4 30. Rxf6 Bc5+ 31. Kh2 Nxf6 32. Qxf6+ Kh7 33. Ng5+ Kh6 34. Ne6 Rxe6 35. Qxe6 Re8 36. Qf6 Be7 37. Qxa6 b4 38. Qc4 Bf8 39. g4 Kh7 40. e6 Bd6+ 41. Kg2 Be7 42. Qc7 Kg8 43. Kg3 Kf8 44. Qf4+ Kg7 45. Qd4+ Kg8 46. h4 Rd8 47. Qc4 Bd3 48. Qc6 Bb1 49. h5 Bxa2 50. Qe4 Kh8 51. h6 Bf6 52. e7 Re8 53. Qf4 Bg7 54. hxg7+ Kxg7 55. g5 Kg8 56. Qf6 1-0
It is always best to play offensive if you are losing, if you play defensive then you will just suffer as your opponent keeps expanding his advantage. Do not take on the offensive so quickly though if you are in a bad position. You have to judge the position correctly and just try to find the best moves, however if it is astoundingly poor then you need to complicate it as much as possible.
The same goes for if you are material down, your best bet is to complicate the position. It would be your only chance against a formidable opponent. This has been proven in practice many times, in one of my games I was a pawn in an early middlegame against an FM but gave up another one(!) to complicate the position and eventually it got extremely complicated and petered out to a draw.