Illegal, but not so quite
First thing to keep in mind is that human memories are not known, only facts. So, while it is illegal to use real memories in replicants, there is nothing prevent a "memory artist" to create an implant from a personal memory that no one but him/herself would know to be true.
Also, replicants now know they have implants, and they serve a psychological purpose, as explained by Tyrell in the first movie. It is not likely a memory implant would be scrutinized or even debated.
So, being illegal does not directly mean that is not widely done.
Different memories for different people
During the conversation with K Stelline is creating a memory, which seems to be from the perspective of a girl in her birthday. It would be logical to give different kinds of memories to replicants of different purposes. A "pleasure model" could have many memories of past love affairs as way of her having "experience", as a Blade Runner replicant could have memories of fighting, being brave and showing strong integrity (they work for the police, after all).
When K asks Stelline what makes "the best memory artist" she says that she "got very good at imagining" because she didn't get to create many true memories. But to a recluse person, even with great imagination, it could be difficult to create a good memory involving violence and bravery; so to do that, she could draw from her true memory from very long ago when she knew the feeling of flee, fright and being beaten.
Part of the plan?
The theory that she could have implanted the memory as part of a plan to throw K off her trail would not make much sense when you taking into account her reaction. She says that "helping in a case" is the most interesting thing he's been asked in years, so we know she does not get many visitations from detectives.
When K is back to headquarters, Madam is screaming mad at him regarding to the place where he was found (outside an "upgrade" center? I don't really recall the exact word), which implies that it is a place off-limits to replicants. That being said, if a replicant would show up, Stelling would know that it is an unique event, and probably something he'd be expecting if it was her plan. She wouldn't, then, react so surprised and off-balance when she sees her memory in K.
Big coincidence, then?
If not part of a plan, it would have to be a pretty incredible coincidence of having K specifically who has Stelline's memory to go see her, right? Actually, not quite. K is not that special, contrary to what he and Joi (and us) believe. Let's recap the events that let to that point:
The tree with the date is in Morton's farm, a replicant which is chased by Blade Runners; so it is likely that, if not K, another replicant Blade Runner would end up investigating and finding the tree and the date. Following the trail, this theoretical other detective would have found the orphanage. Now, there, if he had that memory, would also have found the toy. So, why K specifically would have that memory?
Remember that Stelline is a third party provider, as she states she "takes her freedom where she can", instead of being an employee. She makes the memories and sells to Wallace Corporation; she does not implant the memories herself. If the employer at Wallace Corporation who is responsible for implanting the memories could choose that memory for any number of replicants, maybe because it would be particularly effective for soldiers or police officers or Blade Runners.
Conclusion or TL;DR
There are good points out there making the case that it was part of a complex plan of hers, but I'm bringing some arguments to a different possibility.
- It is a memory only she knows, so there wouldn't exist a way of accusing her of using illegal real memory.
- She does not lie to K
Someone lived this
- K having Stelline's memory and meeting her could very be simply an accident, and even a not very special or serendipitous one at that.
- She did not seem to expect to see that memory coming back to her.
- It is a special kind of memory, not widely used in replicants in general, but for Blade Runners.