In ultraperipheral collisions (UPCs) of relativistic nuclei without overlap of nuclear densities, the two nuclei are excited by the Lorentz-contracted Coulomb fields of their collision partners. In these UPCs, the typical nuclear excitation energy is below a few tens of MeV, and a small number of nucleons are emitted in electromagnetic dissociation (EMD) of primary nuclei, in contrast to complete nuclear fragmentation in hadronic interactions. The cross sections of emission of given numbers of neutrons in UPCs of $^{208}$Pb nuclei at $\sqrt{s_{\mathrm{NN}}}=5.02$ TeV were measured with the neutron zero degree calorimeters (ZDCs) of the ALICE detector at the LHC, exploiting a similar technique to that used in previous studies performed at $\sqrt{s_{\mathrm{NN}}}=2.76$ TeV. In addition, the cross sections for the exclusive emission of one, two, three, four, and five forward neutrons in the EMD, not accompanied by the emission of forward protons, and thus mostly corresponding to the production of $^{207,206,205,204,203}$Pb, respectively, were measured for the first time. The predictions from the available models describe the measured cross sections well. These cross sections can be used for evaluating the impact of secondary nuclei on the LHC components, in particular, on superconducting magnets, and also provide useful input for the design of the Future Circular Collider (FCC-hh).