Tax-free sale
Anyone wishing to sell their rented property tax-free can do so without any problems ten years after purchase.
If the property is sold earlier and a profit is made, this is considered a speculative transaction. A speculation tax must then be paid on the profit. Exception: The property was used "[...] in the year of the sale and in the two preceding years for own residential purposes [...]".
Legally, this is all regulated in § 23 EStG. In addition: If the sale causes the loan to be repaid early, an early repayment penalty must be paid to the bank.