The hot and dense matter generated in heavy-ion collisions contains intricate vortical structure in which the local fluid vorticity can be very large. Such vorticity can polarize the spin of the produced particles. We study the event-by-event generation of the so-called thermal vorticity in Au + Au collisions at energy region $sqrt{s}=7.7-200$ GeV and calculate its time evolution, spatial distribution, etc., in a multiphase transport (AMPT) model. We then compute the spin polarization of the $Lambda$ and $bar{Lambda}$ hyperons as a function of $sqrt{s}$, transverse momentum $p_T$, rapidity, and azimuthal angle. Furthermore, we study the harmonic flow of the spin, in a manner analogous to the harmonic flow of the particle number. The measurement of the spin harmonic flow may provide a way to probe the vortical structure in heavy-ion collisions. We also discuss the spin polarization of $Xi^0$ and $Omega^-$ hyperons which may provide further information about the spin polarization mechanism of hadrons.