Why iOS on iPhones and iPads automatically end an app (ie Youtube) after idling? Is there a way to stop iOS from ending apps when idling?
iOS automatically quits apps which haven't been used for some time, especially if available memory required for the active apps runs low. There is no setting to disable that (which probably wouldn't be a good idea anyway as it would block other apps from getting loaded/started).

But usually apps get notified before they get unloaded and are supposed to store their current state (including for instance the playback position for YouTube) so they can restore it when you switch back to them/restart them. If you need advice on how to make this work for you when using YouTube, please ask a new question (and include the details others asked for in the comments).