The needle pierces the egg's air cell, which helps to reduce internal pressure while it boils. This prevents the egg from bursting in the boiling water.
Using older eggs for hard boiling can prevent the whites from leaking when pierced. Older eggs have larger air cells, making them easier to peel and ideal for boiling.