Depending on whether the engine is hot or cold, there are a couple different reasons why a car might start with no problem but stall shortly afterwards. If the problem only happens when the engine is cold, a bad electric sensor is likely to blame. If the car stalls even when the engine is warm, the cause is likely a faulty electric fuel pump.
A bad electric sensor can only occur on a car with a fuel injection system. Under this scenario, one of the electronic sensors has gone bad and is causing the car's computer to malfunction and improperly calculate the amount of fuel the engine needs. If it's cold outside or the engine is cold, the sensors are not sensing the low temperature, therefore the engine is not maintaining the appropriate air-to-fuel ratio to keep it running.
A faulty electric fuel pump is also a problem limited to cars with a fuel injection system. A whirring sound can be heard coming from the engine when the fuel pump is working correctly. If this pump fails, the engine will not receive enough fuel to keep it running after starting.
A qualified mechanic can troubleshoot, diagnose and confirm the source of the problem. It is a relatively simple job to replace a faulty sensor or electric fuel pump.