We always hear diesel engines 'ping'. This can happen only if the fuel is injected during the compression stroke, not when the piston has reached the top. So, it means fuel is injected before the piston reaches TDC. But wouldn't ignition at that point be counter-productive? since it would go against the motion of the piston.
Most diesel engines work the same way as most gasoline engines, meaning they are 4-stroke engines (this answer will give you more information about diesel and gasoline engines and how they run.) Seeing as how they actually run the same basic way while the ignition source is different (spark v. compression), it makes no difference from this standpoint. In both gasoline/diesel engines, the ignition occurs before top dead center (TDC) of piston movement between the compression and power strokes so as to optimize the power output of the cylinder. In order for all of the fuel to burn, the ignition of the fuel must occur early. It also produces more power than if it occurred at or after TDC.
How do you prevent a lean mixture combustion as the combustion is starting before the whole fuel has been injected?
When the fuel is added to the air, it will only burn as much fuel as there is for air to allow it to burn. If you have too much fuel, you have a rich condition. When this occurs with a diesel engine, you get black smoke. (Many hot rodders call this "rolling coal".) If there's not enough fuel, then you have a lean condition, which means there's excess oxygen in the exhaust. The theoretical perfect mixture is called stoichiometric or sthoic for short. It means there's just the right amount of fuel added to the air for there to be only exhaust fumes without excess air or unburnt fuel. I state this as "theoretical", because in practice the engine (whether gas or diesel) won't run at this level very much. It will vary above and below this level to provide either better power (richer during heavy loads) or better fuel economy (lean during lighter loads). Diesel engines are commonly known as lean burn engines. This is one of the reasons why they get great fuel mileage. I spell all this out so you might understand when the fuel is injected has less to do with whether the engine runs lean or rich. Injecting early does allow the air/fuel to burn more completely before it is evacuated from the cylinder, but to overcome this effect, you'd just have to inject a little more fuel. This is all engineered into how the engine runs, so it's not a big deal.
How is the fuel injected? since it will ignite before the whole pulse is finished, the pressure wave, pushing the piston down, will also push the fuel back toward the pump. I understand that the injector opening represents a small surface area, but the back pressure on the fuel flow must be very strong.
Diesel and gasoline direct injection (GDI) engines have two fuel pumps. One is a low pressure pump, which pulls the fuel out of the tank and delivers it to a high pressure pump. The high pressure pump is designed to provide more pressure at the fuel injection nozzle than would be seen inside the engine. What the pressure inside the cylinder equates to depends on a few factors, such as whether the engine is naturally aspirated or turbo-/supercharged (forced induction). Obviously, if you start out with a higher static pressure before compression, you'll have a greater pressure at the peak of compression. This is all calculated by the engineers and the high pressure pump is designed to make up the difference.
Unlike a gasoline engine, the diesel engine is a slow burn engine, meaning, it takes more time for the fuel to burn than does a gasoline engine. The injection itself can therefore be slower. In the GDI engine, the injector itself needs to inject the fuel at a much faster rate than like the port fuel injection engines of the past. This article states the GM LT1 engine (Gen V Small Block Chevrolet) has injectors which delivers roughly the same amount fuel in 1/4 of the time as the older engines did. Other sources I've read also states these GDI injectors don't deliver the fuel in a single burst, but rather, it modulates the fuel injection to get the best burn and produces more power while getting better fuel economy in doing so. I've not read directly about diesel engines, per se, but would bet they also modulate the fuel output at the injector for the same reasons. In the States, emissions output and fuel economy have hit the diesel world the same as it did the gasoline world a while back, so they are going to use whatever techniques they can to get the best of both worlds.