I just noticed that David Lynch plays one of the major characters in his own show Twin Peaks and it got me wondering about other instances like how Stan Lee appeared in his superhero movies, but only as a cameo. Is there a name for when it happens longer than a cameo appearance like in Twin Peaks?
The trope you are thinking of is: Descended Creator
This is when a crewmember or executive for a show makes an appearance in a part that would ordinarily be cast with a professional actor — or the crewmember is also an actor. This may start out as a Creator Cameo, with their original "extra" part becoming an Ascended Extra.
Your Stan Lee example comes under Creator Cameo trope while David Lynch Comes under the Descended Creator trope.
Is there a name for when it happens longer than a cameo appearance like in Twin Peaks?
Although @NikhilEshvar has already provided a good answer, I believe the As Himself trope also sufficiently describes what you're looking for, and specifically characterizes long term appearances (though, it's restricted to the actor playing themselves).
This trope is when a real-life celebrity or famous figure is playing a fictionalized version of themselves, as a main character or recurring character. This is mostly a television trope, but there are film examples. For shows that take place in an alternate Hollywood, such as Curb Your Enthusiasm, Entourage and The Larry Sanders Show, this is the norm.
Remember, if it's just a short gag, or one episode, that's either a Special Guest or a straight Cameo.