What does the expression “to paint the lily” mean and Where does it come from?

Yes, “to paint the lily” is the way Shakespeare wrote it; not as one so often hears it, “to gild the lily.”

It’s to be found in King John, Act IV, scene 2.

The king who seized the throne unjustly after the death of his brother Richard in 1199 believed toward the close of his reign that a second coronation might strengthen his position and bolster the waning affections of his subjects.

Lords Pembroke and Salisbury, among others, thought that to be an altogether superfluous gesture, and Salisbury added:

Therefore, to be possess’d with double pomp,
To guard a title that was rich before,
To gild refined gold, to paint the lily,
To throw a perfume on the violet,
To smooth the ice, or add another hue
Unto the rainbow, or with taper-light
To seek the beauteous eye of heaven to garnish,
Is wasteful, and ridiculous excess.