Why did writing arise in and spread to some societies but not to others?
Most societies with writing did not develop it on their own. Rather, they "borrowed" it from or were inspired to develop it by neighboring societies who already had writing. If societies were isolated and so were not aware of writing and its value, they were unlikely to create it on their own.
The societies that did independently invent writing were those with complex hierarchies and sophisticated systems of food production. Writing was needed for politics and was generally learned by bureaucrats who had the comfort of available time and energy. As food was available to them, they had no need to hunt and gather for themselves. Since their basic needs were being met, they could focus on a higher level of existence and communication.