The Palladian mansion, built in 1758 by Robert and John Adam for the Fifth Earl of Dumfries, was due to go under the hammer on Tuesday (26 June).
But now a last-minute intervention by Prince Charles, who secured a £45 million rescue package, has saved the day.
Under the agreement, a range of charities and heritage groups donated £25 million to charity the Art Fund. Prince Charles' Charity Foundation then contributed an additional £20 million to secure the sale by the building's owner Lord Bute.
The Art Fund will now hand the house, which lies around 20km east of Cumnock, to a trust that will open the building and some of its 800ha to the public.
SAVES's Adam Wilkinson said: 'The Prince of Wales took up the challenge and ran with it. We are delighted the building has been saved.'