Catastrophic failure
Most likely the source of the wheel going loose was loose nut. Department store bikes are not likely to have quick release which is safer because it does not become loose with time.

Put the wheel back and screw it on tightly. Don't forget to check the front wheel too, because if that goes loose the consequences will be very unfortunate.
The wheel on the picture looks off centre which is likely because it is not properly in.

The question is whether you need need to invest into more serious bike. If cycling is interesting to you, the answer is definitely yes. Just don't buy those bicycle-like objects in department stores anymore.