Say two companies are working on some software that does essentially the same thing: Can one of the companies patent the idea before it's finished to prevent the other company from working on it, or can they only patent the software once they have a working product?
Patenting does not require a working product in fields where the performance is predictable before hand. Chemistry and biology do have criteria regarding real results. The description in the patent application needs to be detailed enough that someone of ordinary skill in the field could make and use the invention.
After company A gets a patent (might be 3 years or never), it can try to stop company B from making, selling, offering to sell, using, or importing whatever is claimed in the issued patent. You say "does essentially the same thing". If you accomplish the identical goal (speaker identification from noisy audio, for example) but accomplish it by following very different steps, then there may be no infringement at all.
(I am no lawyer but an engineer).
As far as I know, you can patent inventions but you can not patent products and you can not patent ideas. An invention is rarely the whole product, but a description of how to build some technical construction that can be used in products.