  We present high signal-to-noise measurements (up to 12$\sigma$) of the
average thermal Sunyaev Zel'dovich (tSZ) effect from optically selected galaxy
groups and clusters and estimate their baryon content within a 2.1$^\prime$
radius aperture. Sources from the Sloan Digital Sky Survey (SDSS) Baryon
Oscillation Spectroscopic Survey (BOSS) DR15 catalog overlap with 3,700 sq.
deg. of sky observed by the Atacama Cosmology Telescope (ACT) from 2008 to 2018
at 150 and 98 GHz (ACT DR5), and 2,089 sq. deg. of internal linear combination
component-separated maps combining ACT and $\it{Planck}$ data (ACT DR4). The
corresponding optical depths, $\bar{\tau}$, which depend on the baryon content
of the halos, are estimated using results from cosmological hydrodynamic
simulations assuming an AGN feedback radiative cooling model. We estimate the
mean mass of the halos in multiple luminosity bins, and compare the tSZ-based
$\bar{\tau}$ estimates to theoretical predictions of the baryon content for a
Navarro-Frenk-White profile. We do the same for $\bar{\tau}$ estimates
extracted from fits to pairwise baryon momentum measurements of the kinematic
Sunyaev-Zel'dovich effect (kSZ) for the same data set obtained in a companion
paper. We find that the $\bar{\tau}$ estimates from the tSZ measurements in
this work and the kSZ measurements in the companion paper agree within
$1\sigma$ for two out of the three disjoint luminosity bins studied, while they
differ by 2-3$\sigma$ in the highest luminosity bin. The optical depth
estimates account for one third to all of the theoretically predicted baryon
content in the halos across luminosity bins. Potential systematic uncertainties
are discussed. The tSZ and kSZ measurements provide a step towards empirical
Compton-$\bar{y}$-$\bar{\tau}$ relationships to provide new tests of cluster
formation and evolution models.
