Was there any "plan B" / solution, in case sealing of capsule hatch, LM hatch and spacesuit helmet failed during Apollo missions or any other spacecraft? How did NASA ensure a failproof sealing in all these cases?
There are so many questions here that I will answer only a few of them. If anyone wants to edit/update please feel free.
Let us take a look at a hypothetical Apollo mission in which one of these systems fails at a time, not all three. The answer to the question 'what happens if a hatch fails to close AND a helmet fails to close' on the moon is "you die". However, there were procedures for what happens if one of those two things fail on the moon.
Excessive Cabin Leak: In the LM operations document Found here, the assumption made is that the crew is wearing helmets/gloves at all times. In the case of an excessive cabin air leak, regardless of whether it is from the hatch not closing properly or what caused it, a sequence of events happens that basically involve the cabin repressurization system to kick in and the astronauts to use their suits until the issue passes (see below)
Helmet won't close in a depressurized cabin: This is covered under the "suit leak in depressurized cabin" portion of the document I linked, since the astronauts would obviously never have their helmets open in a depressurized cabin, the only chance of this happening would be if the helmet seal came undone or something else caused a link. The procedure is for the astronaut without the leak to seal the hatch and repressurize the cabin immediately.
if by some chance the suit leak happened in a depressurized cabin AND it was excessive enough to require emergency procedures to repressurize the cabin AND the cabin could not be repressurized then the astronaut would die...