Question

I have a acura rdx that turns in in june - i will be well over on miles and with my new situation i will be driving a lot and it makes no sense to lease anymore
How does dealership determine how much the car is if i want to buy it?
Or should i turn in and pay mileage penalty and get another car - purchased
Ultimately is it better to buy at end of lease
Thx u very much

John