When an endpoint has some memory - be that physical RAM, control/status registers, Flash, etc. - the system needs some way of knowing how to address that memory. Remember that software running in the system uses memory addresses to access data. By assigning a physical address to the device, the memory can be accessed by software just like any other memory.
The address programmed into the BAR on an endpoint represents the physical memory address in the system that software can use to communicate with the corresponding memory in the endpoint. An EP can have up to 6 of these regions. The lower N bits of the BAR will always be 0, where N is the width of the memory. For example if you had a 64kWord memory on your EP, that would be a 16b wide word address, so the lower 16b of the BAR will be hardwired to 0's.
The EP doesn't necessarily know the address values of the RC. It doesn't initially need to. EPs start as being passive devices - only responding to requests from the RC. If they need to send their own write and read requests, the OS must configure them to be capable of performing bus mastering.
Once capable of bus mastering, software on the system (typically device driver) will configure the device to initiate requests - for example if you wanted to DMA a chunk of data from say system RAM to your device or vice versa, the driver would work out the physical memory address it is required to communicate with and send that information to the EP as part of setting up an operation.
If you have two PCIe devices that are intended to talk directly to each other (P2P), again it would be up to the device driver or other such software to read the BAR addresses for the two EPs and pass that information to both devices so that they know how to address each other.