Lag while playing online

Hello, when I am playing online, I get a lag while playing,

how can I fix the problem?

well you don’t really provide enough info for us to go on.
Not enough to say you get lag, we all get lag at some point.

Where are you in the world?
What country is the server in that you are playing that gives you lag?
What is your PC spec?
What is your internet speeds?
Are you downloading or updating whilst trying to play?

See not just a simple “I get lag”. Many factors can give you lag so please give more info or you will not be helped