If everyone loses their jobs
Not everyone is losing their jobs. Technological innovation usually leads to increased employment, as lower manufacturing costs lead to increased production, and expansion of non-automated jobs. As the cost of manufactured goods fall, people will consume more of them, but also spend less on them, and spend more on services, which are much harder to automate. Currently, China has a much smaller service sector than more advanced economies. That is changing fast.
Because of the one-child policy, China's labor force is already shrinking, and a looming shortage of workers is a far more realistic scenario than "everyone loses their jobs".
who will be able to buy the products?
I doubt if Foxconn's assembly line workers were buying many iPhones. Apple and Foxconn shareholders will have more money to spend. As profit margins go up, the incentive to design additional profitable products will increase, causing higher demand for engineers and programmers. Chinese workers will move up the value chain, just like in every other country that has industrialized.