A vital nursing home in the Highlands has been saved from closure after a deal was struck to take it into public ownership.