Can someone who has a gog version of a game play with someone who has a steam version of the game in online games? There are lots of online games and online co-op games out there that are on steam and gog and I wonder if they can join each other’s lobbies and play each other and etc? Or does one need to have a steam copy to join someone else on their gameplay?
That entirely depends on the game. Some games, like Terraria, can connect to the same servers regardless of whether it was bough on Steam or GOG. Others could rely on Steam's API for matchmaking and online play on the Steam version, and will likely be incompatible with GOG versions. And then there are games that may not be designed for online play, but still can be played online using methods like Steam Remote Play.
If I see on the store page that the game has Steam API, just forget about getting the GOG version because I won’t be able to play with the steam players?
Mostly you have to look it up on a per game basis. Probably just Google the game name and "supports cross play between" whatever platforms you want and someone will know