“It’s a great victory by a great people,” Mr. Moon said. “I’ll gather all of my energy to build a new nation.”

Hours after celebrating his election win with thousands of supporters in wet Seoul streets, new South Korean President Moon Jae-in on Wednesday was quickly thrown into the job of leading a nation deeply split over its future and faced with growing threats from North Korea and an uneasy alliance with the United States.

Mr. Moon, whose victory capped one of the most turbulent political stretches in the nation’s recent history and set up its first liberal rule in a decade, assumed presidential duties after the National Election Commission officially declared him as winner. He will be formally sworn in at noon, forgoing the usual two-month transition because he was chosen in a special election after the last elected office-holder was removed by a court and jailed on corruption charges.

Taking up his role as the new commander in chief, Mr. Moon received a call from Army Gen. Lee Sun-jin, chairman of South Korea’s Joint Chiefs of Staff, who briefed him on the military’s preparedness against North Korea.

A short time later, Mr. Moon stepped out of his private home and received an emotional send-off from hundreds of residents, who hanged flowers on his neck and asked him to pose with his children. He then left with his wife, Kim Jeong-sook, for the National Cemetery in the central city of Daejeon.

After bowing to honour the former presidents, independence fighters and war heroes at the cemetery, Moon wrote “A country worth being proud off; a strong and reliable president!” in a visitor book.

After taking the oath of office at the National Assembly at noon, he is also expected to nominate a prime minister, the country’s No. 2 job that requires approval from lawmakers, and name his presidential chief of staff during the day.

The election commission finished counting as of 6 a.m. and said Moon gathered 41 percent of the votes, comfortably edging conservative Hong Joon-pyo and centrist Ahn Cheol-soo, who gathered 24 percent and 21 percent, respectively.

“The National Election Commission, based on the first clause of Article 187 of the Public Official Election Law, determines that the Democratic Party’s Moon Jae-in, who gathered the largest number of valid votes, was elected as president,” NEC Chairman Kim Yong-deok said in the televised meeting.

South Korea might see a sharp departure from recent policy under Moon, who favours closer ties with North Korea, saying hard-line conservative governments did nothing to prevent the North’s development of nuclear-armed missiles and only reduced South Korea’s voice in international efforts to counter North Korea.

This softer approach might put him at odds with South Korea’s biggest ally, the United States. The Trump administration has swung between threats and praise for North Korea’s leader.

Mr. Moon, the child of refugees who fled North Korea during the Korean War, will lead a nation shaken by the scandal that felled Park Geun-hye, whose criminal trial is scheduled to start later this month.

Without the usual two-month transition, Mr. Moon initially will have to depend on Park’s Cabinet ministers and aides, but he was expected to move quickly to replace them with people of his own. He will not serve the rest of Park’s term but rather will serve out the typical single five-year term.

After exit polls indicated to his victory Tuesday night, Mr. Moon smiled and waved his hands above his head as supporters chanted his name at Gwanghwamun square in central Seoul, where millions of Koreans had gathered for months starting late last year in the peaceful protests that eventually toppled Park.

“It’s a great victory by a great people,” Mr. Moon told the crowd. “I’ll gather all of my energy to build a new nation.”

Moon was chief of staff for the last liberal president, the late Roh Moo-hyun, who sought closer ties with North Korea by setting up large-scale aid shipments to the North and by working on now-stalled joint economic projects.

Park’s trial on bribery, extortion and other corruption charges could send her to jail for life if she is convicted. Dozens of high-profile figures, including Park’s longtime confidante, Choi Soon-sil, and Samsung’s de facto leader, Lee Jae-yong, have been indicted along with Park.

Moon frequently appeared at anti-Park rallies and the corruption scandal boosted his push to re-establish liberal rule. He called for reforms to reduce social inequalities, excessive presidential power and corrupt ties between politicians and business leaders. Many of those legacies dated to the dictatorship of Park’s father, Park Chung-hee, whose 18-year rule was marked by both rapid economic rise and severe civil rights abuse.

As a former pro-democracy student activist, Mr. Moon was jailed for months in the 1970s while protesting against the senior Park.

Many analysts say Mr. Moon likely won’t pursue drastic rapprochement policies because North Korea’s nuclear program has progressed significantly since he was in the Roh government a decade ago.

A big challenge will be U.S. President Donald Trump, who has proven himself unconventional in his approach to North Korea, swinging between intense pressure and threats and offers to talk.

“South Koreans are more concerned that Trump, rather than North Korean leader Kim Jong Un, will make a rash military move, because of his outrageous tweets, threats of force and unpredictability,” Duyeon Kim, a visiting fellow at the Korean Peninsula Future Forum in Seoul, wrote recently in Foreign Affairs magazine.

“It is crucial that Trump and the next South Korean president strike up instant, positive chemistry in their first meeting to help work through any bilateral differences and together deal with the North Korean challenge,” she said.

Moon’s turbulent life

The son of refugees who fled North Korea during the 1950-53 Korean War, Moon, 64, grew up in poverty and was jailed for protesting military-backed dictators. Moon later became a human rights lawyer and worked for late liberal President Roh Moo-hyun.

Childhood

Moon’s North Korean parents resettled in South Korea’s southeast before he was born in January 1953. They initially lived in a POW camp.

As a boy, he often went to a Catholic church with a bucket to get free U.S. corn flour and milk powder.

College

After entering Seoul’s’ Kyung Hee University in 1972, Moon joined a pro-democracy movement to topple the dictatorship of Park Chung-hee, who ruled South Korea for 18 years until his 1979 assassination. Park was the father of recently ousted conservative President Park Geun-hye.

In 1975, Moon was jailed for months for staging anti-government protests before being conscripted into the military’s special forces.

Moon became a lawyer and joined Roh’s law office in the early 1980s. They defended the rights of poor laborers and student activists until Roh entered politics in 1988.

Moon says their friendship changed his life.

‘King Secretary’

After Roh became president in 2003, Moon became what local media called Roh’s “King secretary” or “Roh Moo-hyun’s shadow.”

When Roh was impeached in 2004 over alleged election law violations, Moon served as a defense lawyer before a court restored Roh’s presidential power. After Roh left office and faced a corruption investigation, Moon was his lawyer. Roh jumped to his death in May 2009.

Presidential challengers

Moon says Roh’s death led him to politics. Moon lost the 2012 election to Park Geun—hye by a million votes.

When the corruption scandal involving Park flared last fall, Moon saw his popularity rise amid massive public outrage toward her conservative government.