Afoul means conflict, so baseball players have to stay in line or they'll be in conflict with the rules. A foul ball in baseball is a ball that is out of play when it lands. The word "afoul" has "foul" in it, a term used in baseball. The joke is playing on the word afoul.
Baseball players have to stay in line or they will be afoul of the rules .