WASHINGTON — President Obama said Friday that the last American soldier would leave Iraq by the end of the year, bringing to an end a nearly nine-year military engagement that cost the lives of 4,400 troops and more than $1 trillion, divided the American public, and came to define America’s role in the world.