How is Raycasting implemented?

I know what it is used for, I know what it is for.

What I am essentially asking is what is the mathematical code behind how it works?

I am aware that any custom implementation is probably slower than an official implementation of engines, I'm only interested in how it works.