Echoing other posters: yes, figuring this out is much more complex in bacteria and archaea than eukaryotes.
A short answer is: a common way to answer these questions is by using different types of algorithms that assess the likelihood/probability of various ancestry scenarios (evolutionary trees). In this case, HGT would be a horizontal branch cutting across the pattern of common descent. Probably the simplest principle to understand is that of parsimony: all things being equal, the simplest evolutionary scenario that requires the fewest changes/gene transfers would be favored as the most likely.
For instance, let’s say you have a group A in which all its members had a gene (A1), but it’s sister (most related) group, B, does not. You might ask: does the presence of gene (A1) in group A derive from ancestry or gene transfer? You would then want to look and see if the most recent common ancestor of groups A and B also possessed (A1) or gene (almostA1). If so, you would likely assume that A1 was inherited or that a small mutation led to almostA1->A1 in group A, as this would be most parsimonious. However, if the most recent ancestor does not have gene A1 or anything like it, you might assume HGT is a potential source for gene A1 in group A.
Current phylogenetic methods use pretty complex programs to search through “tree space” (the billions and billions of possible ways that organisms are related) and then assess which relationships are more or less likely. These programs will incorporate models of how evolution occurs based on knowledge from other systems (what types of changes are more or less likely, etc.). You could also incorporate a priori knowledge (we see this gene being transferred via HGT in other organisms as well…seems somewhat common), etc.
In the end, when talking about deep time evolutionary changes, there will generally be a favored, but not definitive, tree (hypothesis). Usually the best we could say is: given what we know, a certain scenario is most likely. But all of these scenarios are really just hypotheses and open to modification with new knowledge in the future.