The original buyer.
For example
if I bought a T-Mobile iPhone 6 for full price from T-Mobile or Apple it would be unlocked. If I then sold it to you, it would still be unlocked.
But if I used a payment plan with T-Mobile instead, it would not be unlocked. At the end of my payments if I sold the phone to you, it would still be locked. Unless I asked T-Mobile to unlock it because my committment had been met.
That's how I understand it to work with AT&T and T-Mobile. Verizon, again, is unlocked no matter who buys it and how much or how little they paid for it initially.