This website explains almost everything there is about raycasting, including how it works and the math behind it:
https://lodev.org/cgtutor/raycasting.html
You could also check out some YouTube videos on it, but that website should answer nearly anything about raycasting.