A search for the production of events containing three W bosons predicted by the standard model is reported. The search is based on a data sample of proton-proton collisions at a center-of-mass energy of 13 TeV recorded by the CMS experiment at the CERN LHC and corresponding to a total integrated luminosity of 35.9 fb-1. The search is performed in final states with three leptons (electrons or muons), or with two same-charge leptons plus two jets. The observed (expected) significance of the signal for W±W±W production is 0.60 (1.78) standard deviations, and the ratio of the measured signal yield to that expected from the standard model is 0.34-0.34+0.62. Limits are placed on three anomalous quartic gauge couplings and on the production of massive axionlike particles.