Many cars in the mid-20th century had their engines in the back, notably the iconic 60s Volkswagen Beetle and Microbus. I know there are certain engineering issues to overcome, but every configuration has compromises, and Porsche has famously still held on to the rear-engine design for their 911. What caused the sharp decline in this configuration?
The main reason is the improvement of front-wheel-drive cars.
If one is using two-wheel-drive, there is the choice between front-wheel-drive and rear-wheel-drive. This is not independent of the choice of where to put the engine - both front-engine front-wheel-drive and rear-engine rear-wheel-drive put the engine where the drive wheels are, and shorten the power train. In particular, they avoid the need for a drive shaft (or drive chain) passing under the passenger space. Front-wheel-drive was experimented with early in the development of cars, but generally, rear-wheel-drive was easier - driving the wheels that are also used for steering adds a lot of extra engineering difficulty, so until about 1960-1980, rear-wheel-drive was the usual choice.
Rear-wheel-drive can also be better for high-performance cars. As the car accelerates, the rear wheels take more of the weight of the car, and the front wheels less. This means that front-wheel-drive cars lose some traction from their drive wheels when acceleration, and rear-wheel-drive cars gain some.
What about engine placement? Rear-engine layouts are rear-heavy, and front-engine layouts are front-heavy. Rear-heavy provides more traction when using rear-wheel-drive, due to having more weight over the drive wheels - this is a good choice for high-performance cars (e.g., sports cars). However, this is bad for stability - when turning while slowing down (whether due to braking or reducing the throttle), the rear of the car wants to keep going straight ahead. For everyday driving, especially by relatively unskilled drivers, front-heavy is safer than rear-heavy.
Put these two together, and we can see why once the chaos of very early car designs settled down into stability, front-engine rear-wheel-drive was the most common layout. The problem with this layout is the long drive shaft connecting the front-mounted engine to the rear drive wheels. This long drive shaft adds weight, and reduced the interior space of the car. Despite these disadvantages, it was still the most common layout, which shows the advantages of front-mounted engines (as noted above, stability is a key advantage).
One thing that drove the shift to front-wheel-drive, especially during the 1970s, was a shift to smaller and lighter cars - largely a shift from big gas-guzzlers to economy cars. Eliminating the long drive shaft reduced weight (and fuel consumption), and gave small cars more interior space. Smaller engines could be mounted transversely, which suits front-wheel-drive (for a front-mounted engine). Transverse engines allowed the whole car to be shorter and lighter. Of course, rear-engine rear-wheel-drive also got rid of the long drive shaft, and worked as a solution, too. If the engine was light enough, stability was not badly affected by the rear-mounted engine - thus, the VW Beetle. However, as the engineering of front-engine front-wheel-drive cars was mastered, rear-engine rear-wheel-drive economy cars largely faded away. (While the VW Beetle is the best-known rear-engine rear-wheel-drive economy car, there were others, such as the Hillman Imp and Fiat 850).
Rear-engine rear-wheel-drive sports cars are still around - such as the Porsche 911. Other than these, there were two major waves of rear-engine rear-wheel-drive cars. First, it was a common layout in the late 19th century, before being largely replaced by the front-engine rear-wheel-drive layout which became the standard for the first two-thirds of the 20th century. Second, rear-engine rear-wheel-drive economy cars, which as noted above, were largely replaced (along with front-engine rear-wheel-drive cars) by front-engine front-wheel-drive cars. (BMW was notable as staying firmly in the rear-wheel-drive camp until their F40 1 Series introduced in 2019 took them into the front-wheel-drive world.)