Photoshop blends two images together by performing a blend operation on each pixel in image A against its corresponding pixel in image B. Each pixel is a color consisting of multiple channels. Assuming we are working with RGB pixels, the channels in each pixel would be red, green and blue. To blend two pixels we blend their respective channels.
The blend operation that occurs for each blend mode in Photoshop can be summed up in the following macros:
While the popular answer is mostly correct, the following statement is wrong. "The remainder of the photoshop blend modes involve converting RGB to HLS and back again." No, Photoshop (and only Photoshop) uses Chroma and Luma instead of HLS.
So for Hue, Color, Luminosity and Saturation modes, you can't use simple algorithms. To match Photoshop's method in these cases, you need to be working for Adobe.
The popular answer is 99.9% correct, but as Greyfriars said, it won't get the exact result because Adobe doesn't use HLS any moment in the blending.
But you don't need to be working at Adobe in order to do that... you can reach exactly the same blending following all the rules here in this document from Adobe: