In a nutshell simply to help them be seen more effectively, yellow ends were a requirement. I believe rules have changed in recent years that allows a train to not have a yellow end if the headlights are above a certain brightness.