why does boiling the water removes the bacteria?

It doesn't remove the bacteria, it just kills them.

BOILING AND PASTEURIZATION. Boiling water kills or inactivates viruses, bacteria, protozoa and other pathogens by using heat to damage structural components and disrupt essential life processes (e.g. denature proteins). ... Cooking food is also a form of pasteurization.