If a masbooq (latecomer) performs the salam along with the Imam, and then completes his prayer based on someone else telling him to do so, his prayer will become invalid. However, if he remembers after performing the salam (even if this remembering is just by looking at the person praying next to him, and then he stands up), then the prayer will not become invalid. (Aham Masail / Vol: 2 / Pg: 85)