The short answer
NATO’s combat mission in Afghanistan, called ISAF, ended at the end of 2014. A smaller NATO-led training and advisory mission then continued until the final withdrawal in August 2021. So the answer depends on which mission you mean.
ISAF ends in 2014
Through 2013 and 2014, Afghan forces took over security responsibility across the country. ISAF formally closed at the end of December 2014. Its troops had been fighting alongside Afghan units for years, and the handover moved them into a support role.
Resolute Support, 2015 to 2021
On January 1, 2015, the Resolute Support Mission began. It trained, advised and assisted the Afghan security forces, and it did not lead combat operations. A separate US counterterrorism force remained in the country, which is one reason the end of ISAF did not mean the end of foreign military action.
The final withdrawal in 2021
The US and the Taliban signed the Doha agreement in February 2020. In April 2021, President Biden announced that the withdrawal would be completed by September 11, 2021. The pullout was accelerated, and the Taliban took control of Kabul on August 15, 2021. The last US and allied troops left on August 30, 2021.
What “ended” means
People often use one date for the whole mission, but there were three separate endings. The combat mission ended in 2014. The advisory mission ended with the 2021 withdrawal. The final exit of foreign forces came in late August 2021. For the wider story of how these phases fit together, read the history of NATO in Afghanistan.