We propose and implement a scheme to produce long-lived entanglement between a signal field and a magnetically insensitive collective excitation in an atomic cloud cooled in a one-dimensional optical lattice. After a programmable storage time, we convert the spin-wave excitation into an idler field, and demonstrate violation of Bell's inequality for storage times in excess of 3 ms.