Tax-loss harvesting works by selling underperforming shares to realise losses, which can be used to offset capital gains from other investments. If the losses exceed the gains, the remaining loss can be carried forward for up to eight years, as per Indian tax rules.