More than half a million people spent 12 hours or more on a hospital trolley waiting for an NHS bed last year.