When a piece is attacked is lesson 1 of 3 in Safe pieces: protect, escape and check, a basics course suited to players rated around 320 (complete beginners). Most early games are lost by leaving a piece where it can be taken for free. Learn the three answers to an attack. It has 4 interactive exercises: 3 find-the-move puzzles, 1 board-square exercise. You play every move on a board and get feedback straight away. The lesson is free.
What this lesson explains
Black's pawn on b5 attacks your rook. If you do nothing, the pawn takes it, and you lose 5 points for 1. When one of your pieces is attacked, you have three answers. Move it away, protect it, or take the attacker.
What you practise
The rook cannot take the pawn, and nothing can protect it in time. Move it away, to a square where no black piece can take it.
The black knight attacks your pawn on d4. A pawn cannot step back, and d5 is blocked. So protect it instead.
The black knight attacks your queen. You could move the queen away, but there is a better answer. Take the attacker.
Make this a habit before every move. Look at your own pieces. Tap each white piece that a black piece can take for free, because nothing protects it.
Worked examples
The rook cannot take the pawn, and nothing can protect it in time. Move it away, to a square where no black piece can take it. Safe. Before you put a piece on a new square, check that no enemy piece can take it there.
The black knight attacks your pawn on d4. A pawn cannot step back, and d5 is blocked. So protect it instead. Good. Now if the knight takes on d4, your pawn takes the knight back. A knight for a pawn is a bad deal for Black.