A nightwatchman is a less-skilled batter who begins playing at the end of the day when conditions are challenging. The idea is that this is better than risking a better batter being dismissed rather than starting play the next day.
Nightwatchmen exist mainly because many games of cricket take place at times of year when it begins getting dark towards the end of the scheduled playing time. Poor light makes it harder to see the ball and thus disadvantages the batter.
If a batter is dismissed, they are normally replaced by the next player in the batting order. A specialist batter is thus normally replaced by another specialist batter, or at least somebody with decent batting ability. However, the combination of declining light, increased bowler confidence and the normal vulnerability of new batters who have not yet “played themselves in” means there’s an increased chance of another dismissal before the end of the play. This would be a significant loss if it were a specialist batter.
Instead, a nightwatchman can come out to bat. This is a lower order batter, usually a specialist bowler. Their main task is to avoid getting out rather than try to score runs. If they are dismissed, it’s not as much of a loss.
The goal is that they survive until the following day, at which point they and the other batter can play as normal. If and when the nightwatchman is dismissed, the team can revert to the normal batting order.
Teams only normally use a nightwatchman when they have been batting for some time. If a team starts their innings toward the end of the day (particularly if the opponents have deliberately declared to engineer such a scenario), they’ll usually start with their normal opening batters rather than a nightwatchman. That’s because the combination of that batter’s tiredness from having just fielded (and possible bowled), and the bowler’s advantage with the new ball make it so challenging to bat that a nightwatchman would likely struggle even to play defensively.