The Sun has lithium because, like all the elements that it has heavier than helium, it was born with it.
Many stars, particularly older metal-rich stars or cool stars with deep convective envelopes show very little Li in their atmosphere - and that is true for the Sun too. But this trace of lithium is still detectable (just).
By comparison with meteorites, we know that the Sun was born with about 100 times the abundance of Li that is seen in its photosphere now. Roughly speaking, about 20% of this Li was produced by big-bang nucleosynthesis and the rest was likely produced, before the Sun was born, in (giant) stars and (classical) novae via rather complicated synthesis and mixing routes.
Most of the Sun's Li has been destroyed in its interior. It reacts with protons to form helium at temperatures lower than required for hydrogen fusion. However, the exterior of the Sun has not been completely mixed with the interior, so a little still remains in the photosphere.
As to why stars with planets deplete their Li more efficiently, well there is still a bit of a question mark over whether that is true or not. If it is, then it probably has something to do with the history of rotation and angular momentum loss in the star. Stars with long-lived, planet-forming discs may have spun down more readily, creating an internal differential rotation that aids the mixing process. It is a clear observational result, that slower-rotating stars in young clusters have depleted more Li than their faster rotating siblings.