Not all of them do. The pupils of domestic cats contract to slits in bright light, but large cats like lions and tigers have round pupils like ours. Similarly, snakes like pythons and boas have pupils that close as slits, but many others – like grass snakes – don’t.
Slit pupils close more tightly, so can handle a broader range of light conditions. Comparisons between snakes support this idea: species active purely in the daytime don’t have slits. In bright light, slits also produce more depth-of-field horizontally than vertically. That could explain why they’re typical of ambush hunters, which need to detect prey moving across their field of vision. Additionally, slit pupils are seen in vertebrates that have ‘multifocal’ lenses, with different areas focussing different colours. Slits mean that more colours can be seen in bright light.