Bruce, when you enter the amount (10,000 lifetime max) of a qualified first home purchase on line 20 of Form 8606, that amount comes out first and is treated as qualified. Next out is the 20,000 of basis. You would not show more than 5000 on line 20 to avoid wasting the other 5000, which can be preserved for the future, so 5000 of earnings would be left in the Roth.
Some people probably enter 10,000 on line 20. This exhausts the lifetime limit, distributes the 10,000 in earnings and leaves 5000 of basis in the Roth IRA instead of 5000 of earnings. However, the next year of Roth distributions taken will result in the line 22 basis amount reverting to 0 per the 8606 Inst for line 22 and the 5000 balance will again be treated as earnings. This effectively means that for the purchase year your earnings were fronted to you tax free, but the status of your Roth going forward does not permanently benefit, since that 5000 was only a loan of your earnings. Of course, eventual tax is avoided on the earnings left in the Roth once it becomes qualified.
That said, in your example, if you intended to close your Roth for good, then you would enter the 10,000 on line 20 and would get your earnings out tax free and there would not be any later contributions that would be treated as earnings by the 8606 Inst, so your entire Roth would have been essentially treated as qualified permanently. The IRS Pubs do not even attempt to explain this.