A judge has ruled the head of a federal watchdog agency must stay in his job, saying President Trump's bid to remove the special counsel was unlawful.