Ships' sails were often dyed red in a process called tanbarking, which was designed as a layer to protect the fabric against the elements. It's made out of tree bark, which was not terribly hard to come by.
As far as other colours are concerned, I think that it's artistic licence. The image you're linking is of the conflict between the Serapis and the Bonhomme Richard during the Battle of Flamborough Head, one of the actions during the Anglo-French War (more information here. No other depiction of either of these ships, or indeed of any ships contemporary to that period (that I have found) shows them with coloured sails at any time. Dyes were indeed expensive, and likely not to have been used for this purpose - the colour would have probably not been strong enough to make any difference.
(edited because I saw a moth and I wrote 'moth' instead of the rest of my sentence)
I own a physical copy of Naval Warfare in the Age of Sail by Bernard Ireland. The book contains a large number of color plates of contemporaneous paintings of sailing ships and battles. All of these paintings show sails that are the usual off-white color of sails, with some dirtier and more towards brown - but none with any color whatsoever.
However, there is a single plate that depicts the battle between Belle Poule and Arethusa in 1778, with the ships decked in multi-colored sails. Rather than a painting, though, it is a woodblock print, just like your image. While the artist who scribed the block was extremely knowledgeable about naval architecture (even getting the apple-cheeked bow of the French ship correct and all of the sails in fine detail), whoever did the actual printing (manually applying the inks to the block and pressing) was probably not.
The result is similar to colorization of old black and white photos, where the person doing the colorization has to guess what the color of some things is. So this appears to be just an artifact of one particular image reproduction technology. It's even possible that the color comes from a modern (or at least post-age-of-sail) reprinting of what was originally a black-and-white block printing.