TOKYO — Shinzo Abe declared victory in national elections on Sunday, ensuring his place in history as Japan’s longest-serving prime minister at a time when the country faces numerous challenges, including a rapidly aging population, tensions with its neighbors in Asia and coming trade talks with an unpredictable counterpart in the White House.

Mr. Abe’s conservative governing coalition won a majority of seats in the upper house of Parliament.

But in a setback for Mr. Abe in an otherwise victorious election, his coalition did not secure the number of seats needed to fulfill his long-cherished ambition of revising a pacifist Constitution that has been in place since American occupiers created it in 1947.

“We have been able to gain a majority,” Mr. Abe said as results were being tabulated on Sunday night. “And I believe that the voters wanted a stable foundation in politics.”