Because different genes are switched on and off in each type of cell.
The diploid number is restored when the two haploid cells combine to create a zygote. Basically when the sperm cell enters the egg cell and makes the offspring.
This is possible because proteins are created from different combinations of amino-acids. While we only have 20 different kinds of aminoacids, these 20 kinds can create thousands of combinations that we can observe in our bodies and bodies of other mammals as well as other organisms.