The unusually narrow confirmation came after questions from members in both parties over Hegseth’s qualifications.