YTM is an internal rate of return. It bundles the coupons, the timing of those coupons, and the gain or loss from buying away from par-value into one annualised number so bonds with different structures can be compared.
Two assumptions hide inside it: you hold to maturity, and you reinvest every coupon at the same YTM. Neither is usually true, which is why realised returns differ from the quoted yield. That gap is reinvestment-risk.
Example: a 5-year bond, 4% coupon paid semi-annually, priced at 96.50. Solving for the rate that discounts $20 every six months plus $1,000 at the end back to $965 gives roughly 4.79%. The 4% coupon plus the pull to par supplies the rest.
Related: current-yield, yield-to-call, yield-to-worst, reinvestment-risk, bond-price-yield-relationship