As mentioned in the comments, this topic can become exceedingly complex if all the aspects of current-gen photochromic lenses are brought to the table (and I'm sure a lot of it is locked away in trade secrets). My goal here is to provide some insight into the chemical/physical mechanisms at work.
Photochromic sunglasses are a smart new application of very old knowledge. Silver salts have been isolated and manipulated for centuries, so it's surely been known they have a tendency to darken in light for a long time. This simple fact (plus a lot of auxiliary processes) was the basis of the main forms of photography all throughout the 19th and 20th century.
Because of this, the classic examples of photosensitive compounds are the silver halides ($\ce{AgX}$), particularly $\ce{AgCl}$, $\ce{AgBr}$ and $\ce{AgI}$. These compounds are colourless to slightly yellow when pure. Upon exposure to photons of appropriate frequency, the idealised sequence of events is:
$$1) \quad \ce{2 X- \xrightarrow{h\nu} X_2 + 2 e-}$$
$$2)\quad \ce{2 Ag+ + 2e^- -> 2 Ag^0}$$
So essentially, illumination of the silver halides triggers the production of elemental (i.e. metallic) silver. This is a photoredox process; we say the halide anions undergo photooxidation, and the silver cations undergo photoreduction. An interesting way to think of it is that the halides normally are not strong enough reductants to trigger the reduction of $\ce{Ag+}$ to $\ce{Ag^0}$. However, this is only true in the electronic ground state of these species - the halide ion $\ce{X-}$ in an electronic excited state has different chemical properties during its short life, and in particular it becomes a much stronger reductant, and now can reduce silver directly. This leads to another description, which can be more accurate in other contexts
$$1) \quad \ce{2 X- \xrightarrow{h\nu} 2X^{-*}}$$
$$2)\quad \ce{2 Ag+ + 2X^{-*} -> 2 Ag^0 + X_2}$$
Back to the silver. Instead of forming a shiny reflective film, the silver is formed as a multitude of tiny disconnected particles. As the photons bounce around chaotically in the maze of silver clusters, a large fraction of the incident light ends up getting absorbed (and turned into heat). On the macroscopic scale, this makes the material appear dark.
It's interesting to point out here that the element silver alone is not responsible for the darkening effect. The process starts with the absorption of light to generate a strongly reducing species, from which the silver ions then steal an electron to become silver metal. In principle, therefore, it is possible to tune this effect depending on what else is mixed with the silver ions. Indeed, as the halides get heavier, ionisation becomes easier (or alternatively, it becomes easier to reach the strongly reducing photoexcited state), and the silver halides become more photosensitive. However, it's possible to mix in something else entirely, such as electron-rich organic compounds, which could then surrender their electron to the silver ions when exposed to light. It's also not fundamental to use silver at all - all metals are dark when dispersed into very tiny clusters.
So we now put this knowledge together into one of the simplest examples of photochromic lenses. This technology may have been used in the past, but is now likely supplanted.
Like before, we start with a silver halide. For example, during the fabrication of glass, a small amount of $\ce{AgCl}$ can be added. Though it would look like otherwise ordinary glass, when struck with light, it can potentially trigger the formation of $\ce{Ag^0}$ and darken the glass.
The problem now is to make this process reversible. In the ideal reactions above, the formation of $\ce{Ag^0}$ would also require the formation of $\ce{Cl_2}$. These two compounds can in principle react to regenerate $\ce{AgCl}$ (after all, $\ce{AgCl}$ was the thermodynamic minimum, it had to be hit with a photon to turn into something else!). However, $\ce{Cl_2}$ could potentially trigger a number of other reaction, or move around in the glass (it's a gas in standard conditions), which makes the process unlikely to be reproducibly reversible.
To solve this problem, it's possible to add another pair of electrochemically active species. In particular, it turns out $\ce{CuCl}$ is a nice choice. Why? $\ce{AgCl}$ and $\ce{CuCl}$ can be mixed into glass without reacting, once again generating a colourless material. However, upon exposure to light, the following (idealised) reactions happen:
$$1) \quad \ce{ Cl- \xrightarrow{h\nu} Cl^{0} + e^-}$$
$$2) \quad \ce{Ag+ + e^- -> Ag^0 }$$
$$3) \quad \ce{Cu+ + Cl^0 -> Cu^2+ + Cl-}$$
Now the chloride is essentially a spectator species. The process could be rewritten as:
$$\text{Net ionic equation}: \quad \ce{Cu+ + Ag+ \xrightarrow{h\nu} Cu^2+ + Ag^0}$$
Over time, the tendency is for the $\ce{Cu^2+}$ to reoxidise the silver metal back completely to $\ce{Ag+}$, thus making the glass transparent again. This process is comparatively slow, however, and therefore while there is light shining on the glass, there is a buildup of $\ce{Ag^0}$, making it dark. All in all, very convenient, indeed.
Lastly, I'll briefly address why this process happens in sunlight, but not indoors. Unsurprisingly, this is an entirely intentional property. Good eye-wear glasses block invisible and damaging UV photons present in sunlight, but shouldn't block visible photons. Meanwhile, a good visible light source should create visible photons, and avoid wasting energy on harmful UV photons. The design of the materials used in both photochromic glasses and fluorescent lamps has therefore naturally lead to the situation where photochromic lenses should generally not darken with indoor light sources.
For a more hands-on approach to this whole topic, I highly recommend this excellent video.