I was playing Left 4 Dead 2 and I saw a player paint a graffiti on a wall (I also saw it done on Team Fortress 2)
How is this done? What is it purpose?
Is it different on those two games or is it something at the engine level?
The default key for sprays is T in TF2. It is likely the same in L4D, though if you look through the keyboard settings it should be there.
Bear in mind that some servers restrict sprays to avoid objectionable (porn or other shock) sprays. If you're on such a server, when you attempt to throw up your spray, the noise will play but nothing will happen.