Ants live in big colonies, and are highly organized. When an ant identifies some food, it picks a small piece of it, and leaves chemical scents called pheromones along the path, which helps the other ants to reach the source of food. As the ants follow the scents of the pheromones, this make them walk in a straight line.
But you know what the ants have different sets of colonies, and each colony has a distinctive set of pheromones. If two ants hit, they smell the scents of each other’s pheromones. If the pheromones don’t belong to the colony, the outsider ant might get attacked. But if the pheromones belong to the same colony then other ants behind to follow the smell and return to their colony.
Now, as these pheromones evaporate quickly, every ant which is carrying food and returning to the colony leaves additional pheromones.
Along with this, sometimes we see ants bumping their heads or colliding with each other while walking, this is because to identify their fellow ants.