The associated production of a W and a Z boson is studied in final states with multiple leptons produced in proton-proton (pp) collisions at a centre-of-mass energy of 13 TeV using 137 fb$^{-1}$ of data collected with the CMS detector at the LHC. A measurement of the total inclusive production cross section yields $\sigma_{\text{tot}}$(pp $\to$ WZ) = 50.6 $\pm$ 0.8 (stat) $\pm$ 1.5 (syst) $\pm$ 1.1 (lumi) $\pm$ 0.5 (theo) pb. Measurements of the fiducial and differential cross sections for several key observables are also performed in all the final-state lepton flavour and charge compositions with a total of three charged leptons, which can be electrons or muons. All results are compared with theoretical predictions computed up to next-to-next-to-leading order in quantum chromodynamics plus next-to-leading order in electroweak theory and for various sets of parton distribution functions. The results include direct measurements of the charge asymmetry and the W and Z vector boson polarization. The first observation of longitudinally polarized W bosons in WZ production is reported. Anomalous gauge couplings are searched for, leading to new constraints on beyond-the-standard-model contributions to the WZ triple gauge coupling.