I've been very curious about this too.
I believe there are two basic approaches.
The first is "lenticular lenses" -- the same technology used to make those shiny postcards that change depending on what angle you tilt them at.
Here's a site with an example GIF and kinda explains how it works:
https://setosa.io/blog/2014/07/07/lenticular/
Normally, the lenticular lens is glued directly onto a printed sheet of paper, but you can instead glue it onto a _screen_, which lets you change the pixels underneath. There are a lot of challenges here; for example, the pixels are usually located a bit behind the glass, so the standard lenses used for printing won't focus perfectly on a screen.
There's also another, simpler approach called "parallax barrier". This is what the 3DS used. Instead of using lenses to bend light, it _blocks_ the light coming out of certain pixels at certain angles. It's basically just an opaque sheet with periodic slits (the 3DS uses an LCD, so that 3D can be turned off by becoming transparent). Before I found a video of someone making one themselves using an iPhone and transparency sheets, but I can't find it at the moment...
3DS parallax barrier: https://www.youtube.com/watch?v=D-LzRT7Bvc0
Diagram from Wikipedia article: https://en.wikipedia.org/wiki/Parallax_barrier#/media/File:P...
A big disadvantage of parallax-barrier is that you end up blocking a lot of the screen, making the image darker.
Supposedly you can combine the two approaches to reduce the darkness.
https://www.youtube.com/watch?v=yetX2C3AdCI
There are probably even more tricks involved, and this is all a lot easier-said-than-done.
Here's a project that uses an LCD to make a dynamic parallax-barrier, that increases brightness by skipping the barriers wherever they don't matter:
https://www.youtube.com/watch?v=YrRnURU1W3E
Tangent: There's also been some attempts to do _the reverse_, and use distorted lenses in front of a camera to recover a 3D scene using only a single camera:
https://dl.acm.org/doi/10.1145/3072959.3073589 (link has a SIGGRAPH talk recorded which shows example results)