How does an op-amp know where ground is?
How does anything know where ground is? Ground is just a symbol we stick on the schematic to make it easier to read. None of the components in a normal circuit read the schematic, so none of them know where ground is.

In the case of op-amp B, the output voltage will be either the maximum voltage the op-amp can output (limited by the supply rails), or the minimum, depending on the polarity of the voltage source on the input.

And building such a circuit in practice, you'd have a problem: there's no path from the voltage source on the input to anything else. As such, the actual values there will be defined by the input bias current of the op-amp and other non-ideal behaviors, so what you'll get is something strange that's mostly a function of the details of that particular op-amp.

You'll probably find it easier to think of op-amps not as amplifying the difference between their terminals. In practice, op-amps are usually operated with negative feedback: when they aren't, they tend to be called comparators. So, the op-amp tries to adjust the output voltage such that the two inputs are equal, and for the ideal op-amp with infinite gain, this is exactly the case: the inputs will always be at the same potential.