Barney and Lily
From Barney's perspective, he most likely sees Lily as his best confidante despite knowing that she can't keep secrets. Barney lets her know that his childhood dream was to be a professional violinist. Also, Lily was the first person he lets know that he developed romantic feelings for Robin. He took her advice on how to better his relationship with Robin so she could see him in a different light. He will sometimes treat her like one of the guys, such as when he tells her dirty jokes that are really only found funny by guys. Barney has shown sexual attraction to Lily. When he first saw her, he called Lily "hot". Marshall also has noted that Barney has tried for a long time to see Lily's breasts. Despite this attraction, Barney would never act on it because he sees her and Marshall as "perfect together", shown when he secretly went to San Francisco to get her to go back to Marshall and with his emotional breakdown to when he, Ted, and Robin believed that she and Marshall were getting a divorce.
From Lily's perspective, she most likely sees Barney as a surrogate brother. She is shown throughout the show reprimanding Barney, both physically and verbally, whenever he does something insulting to women that she doesn't approve of. An ongoing catchphrase in the show is that whenever Barney does something that truly upsets Lily, such as blackmailing her, she always says, "You son of a bitch!". Lily has shown a particular interest in Barney's romantic life. Despite the other members of the gang believing that Barney is perfectly happy being single, she believes that deep down Barney does want to get married and start a family someday. She is always excited when Barney shows legitimate feelings for women, as seen with Robin and later Nora and Quinn. Whenever this happens, she pushes him to pursue a committed relationship. Regardless of their sibling relationship, Lily seems to have an interest in his sexual activities. She expresses a bizarre obsession with Barney's sex life, shown when she asked Robin how he was in bed and if he liked using sex toys after sleeping with him for the first time. This, though, would appear to be purely intrigue as she has shown quite the opposite of desire to interact with him sexually herself.
Lily and Barney may not see eye to eye, but they do share some similar traits.
They are the only true New Yorkers amongst the group (Lily was born in Brooklyn and Barney is from Staten Island), with Ted being from Cleveland, Ohio, Robin from Vancouver, British Columbia, and Marshall being from St. Cloud, Minnesota. They also both have issues with their respective fathers. Lily's father paid almost no attention to her when she was a child and Barney's father was absent throughout his entire childhood (posing as his uncle until he left when Barney was 6), with Barney not meeting him until Legendaddy. They both have pardoned their fathers.
Both display sexual ravenousity within themselves, Lily having a lust for getting her husband Marshall into bed on a frequent basis (Purple Giraffe serves as an example) and Barney's lust towards getting women in general into bed on a frequent basis is pretty self-explanatory. Both also have been known to experiment, such as with Barney's Stormtrooper and when Marshall describes his being greeted home by Lily wearing nothing but whipped cream and a pink thong.
They have both been arrested on separate occasions for public urination. (Belly Full of Turkey)
Shortly after Barney met Ted and Marshall, he and Lily met in 2001. After learning that Marshall has only had sex with one woman, Barney wanted to teach him how to live, but being committed to Lily, Marshall refused his offer. Barney then asked Marshall if his girlfriend was as hot as the redhead that just entered MacLaren's, who happened to be Lily. When Marshall and Ted saw that the woman Barney was talking about was actually Lily, they decided to play a trick on him. The two of them acted out a skit, where Marshall pretended that he wanted to have sex with someone new and Ted tried to dissuade him from it. Barney, excited that Marshall was taking his advice, gave him a "pep talk" where he told Marshall that he could do this, but most likely that he probably couldn't, and that the important thing was that hitting on the hot redhead would boost his confidence. As Marshall left to hit on Lily, Barney laughed and told Ted that Marshall was going to crash and burn. Barney was incredibly surprised and spat out his drink when he saw Marshall make out with Lily. He then told Ted that Marshall was a god among men. Marshall apparently allowed Barney to believe that Lily was some random girl for about a week. It is not known how Barney and Lily formally met.
Before Season 1
According to How I Met Everyone Else, Barney first met Lily when he was talking to Ted and Marshall. Barney convinces Marshall to cheat on his girlfriend with "the hottie who just walked in". This turns out to be Lily, and Barney is amazed when Marshall and Lily begin making out. According to Ted and Marshall, for a week Barney asked Marshall to teach him how to live and even did his laundry.
Barney introduces Ted to Robin with his "Have you met Ted?" line, which eventually led to her joining the gang and becoming Lily's best friend. Later, Lily would run into Barney's ex-girlfriend, Shannon, who gave her a video tape to return to Barney. Being curious about what's on the tape, she gives Barney a decoy and then she and the rest of the gang plus Victoria decide to watch it after seeing Barney destroy it. They play the tape and see a younger, long haired Barney crying and begging for Shannon to take him back, this upsets Barney so he runs away from the gang. When they find him at MacLaren's, they get to learn more about Barney's background in exchange for their most humiliating stories, including Lily's story about how she and Marshall had sex while Marshall's mother was on the phone and heard the whole thing. After Barney reveals that he saw Shannon recently and learned that she had a son, Lily believes that he is upset that Shannon's son isn't his. When Barney then tells the gang how relieved he is that he and Shannon broke up and how he "nailed" her afterwards, Lily believes he is lying till he shows her the video recording of them having sex that he took on his cell phone. Later, Barney would accompany Lily and Robin to a high school prom to listen to a band that Marshall wants for their wedding.
In Best Prom Ever, Barney escorted Lily and Robin to a prom in New Jersey in hopes of hearing the band play. Lily and Robin manage to get in, but Barney is left outside.
In World's Greatest Couple, Barney lets Lily stay over at his place. Barney gets annoyed with Lily when she buys groceries, saying that he would be in a relationship if he wanted a refrigerator full of groceries and fresh coffee in the morning. He then gives Lily a tour of his apartment, full of things that try to scare off relationships, such as a king-size bed with a full size blanket and one pillow, a toilet seat which will not remain down, and professionally lit bookshelves of porn. When one particular woman is not deterred by any of these things, Barney finds Lily helpful, as she can pretend to be his wife and scare off the woman. Barney then insists that Lily stay, which she will do only if he allows her to give his apartment a slight makeover so that it looks more like a woman lives there. So she redecorates and makes it look very un-Barney-ish. Lily and Barney sleep in the same bed and merely cuddle together, Barney realizes that he and Lily are basically in a relationship, and he wants out. Lily gets to keep anything she picked out for Barney's apartment and takes it to her own apartment, and lives there instead of living with Barney. Although things don't work out with them, she actually managed to get Barney to be a normal guy.
In Bachelor Party, it is revealed that Barney is mostly responsible for Lily changing her mind after she has left New York between Come On and Where Were We?. When Barney sees Marshall crying over Lily at some point during her absence, he immediately leaves MacLaren's (much to Ted's dismay), only to secretly board a plane to San Francisco. He finds Lily's apartment and, without letting her say a word, demands she come back home. He assures her that she and Marshall belong together and have something most people search their whole lives for and never find. He knows that she loves him and has even bought her a ticket home. Admitting he cannot stand the thought of Marshall finding another girl than Lily, he fears that this would eventually happen and he "can't keep stealing chicks from him forever". He has also insisted that his visit remains a secret between the two and is visibly embarrassed when Lily shares the story with Marshall and the gang.
In The Bracket, Lily is approached by a woman, who tells her not to sleep with Barney, mistaking Lily as one of his dates. She accompanies him on his search for the woman and hopes Barney will use this to apologize to each woman, which he does not. After a woman walks towards him, he gives her an apology. She turns out to be an acquaintance of Robin, but Lily is nonetheless proud of Barney, giving him a hug.
In Do I Know You?, Barney confides in Lily that he has feelings for Robin. Overjoyed, Lily sets the two up on a date under the guise of a hangout night for all of them. Barney does show Robin a different side of him, but she sets him up with a waitress and leaves. Lily is disappointed at this, thinking Barney won't change and leaves.
In Shelter Island, Barney and Lily make a bet that he cannot restrain himself from having sex with the other wedding guests as he said he wants to sleep with Robin when she arrives from Japan.
In Definitions, Lily locks Barney and Robin in Robin's bed room, and doesn't let them come out until they define their relationship.
In A Change of Heart, Barney asks Lily to accompany him to a heart scan. Finding some abnormalities and upon explaining the events of the night, Lily (and the doctor) take turns slapping Barney. But Lily realizes Barney had a moment where his heart literal skipped when Nora entered the restaurant.
In Legendaddy, when Barney leaves his dad's house, saddened, Lily and the rest of the group comfort him as he looks longingly out the window.
In Ducky Tie, Marshall and Lily make a bet with Barney that could force him to wear Marshall's ducky tie.
In No Pressure, Barney is on a mission to find something private of Marshall and Lily.