Denmark and the US have reached an agreement after months of threats from Donald Trump to annex the territory.