Here is an example:
The Author agrees to hold harmless and indemnify the Journal and The
University against any legal claim or action or expense of any nature
arising from any claim of infringement of copyrights or proprietary
rights resulting from publication of the manuscript or claims of
libel, obscenity, unlawfulness or invasion of privacy arising out of
anything contained in the manuscript as furnished by the Author.
Suppose Author infringes the copyright of Jones, by copying large parts of it into Author's work. Author is now in legal trouble because he illegally copied stuff into his manuscript, but Journal is also in big (bigger) legal trouble, because it made many copies of Jones' word and sold them. Jones will now sue everybody, mostly the Journal (since Journal has money, and Author doesn't).
Thanks to the indemnity clause, when Journal gets sued, all of the costs (of litigation and judgment) have to be born by Author. The primary purpose is to protect Journal from suits by third parties. Nothing can keep you from getting sued, but such a clause (theoretically) means that the person whom your contracting with has to cover the cost of his wrong-doing (assuming that he is not a turnip).
The term "hold harmless" is there to guarantee that Author can't decide to sue Journal for publishing a libelous or infringing article. The functions are similar, but not totally the same: this and references therein could be interesting reading, by way of more details. He argues against using both terms, and instead you should use only "indemnify". In this case, the court said "When two words are used in a contract, the rule of construction is that the words have different meanings", which caused the court to assign distinct meanings to the words (which are typically used as though they mean the same thing), which doesn't seem to have been the original intent.