Like Jacob, Joseph, and Moses, Joshua was privileged to know when his end was upon him. So, he blessed and instructed the nation as those patriarchs had done before him. Now, since he was speaking under the Spirit of prophecy, his words are more than just the emotional exclamations of a 110-years-old man. Let's consider his words... some of which were actually God's words.
Joshua called for an assembling of the people before God. Through Joshua, God reviewed the past. He spoke generally of the events from the days of Abraham, Isaac and Jacob all the way through to the time of Moses. God reminded the people of how He had used hornets to drive out some of their enemies from Canaan. God emphasized to them just how blessed they had been in receiving houses, lands and farms that were fully ready and operational.
And, based upon all of that, beginning in verse 14 Joshua challenged the people to simply SERVE GOD continually. His most famous words did indeed come at the very end of his life. One might fairly assume that it had been a mantra of his for many years (Joshua 24:15).
With wholesome intentions, the people of Israel repeatedly assured Joshua that they would be good ... that they would indeed serve God (Joshua 24:16, 18, 21 & 24). Strangely, Joshua challenged their claim (Joshua 24:19-20). As it turns out... Joshua was right (Joshua 24:31).
It seems to be the inevitable plight of mankind. Every church, every educational institution, every nation... it seems like all go astray eventually. And, no surprise I guess. We are, after all, a fallen race (Isaiah 53:6, Psalm 78:39 & Jeremiah 17:9). Only God is immutably good. We should be deeply grateful that His righteousness is offered to us as a free gift.